注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 Alex-just 于 2021-3-2求大米,求积分
- public class BuyingShowTickets {
-
- public static void main(String[] args) {
- List<Integer> tickets = Arrays.asList(5, 5, 2, 3);
- int p = 3;
- long res = waitingTime(tickets, p);
- System.out.println(res);
- }
-
- public static long waitingTime(List<Integer> tickets, int p) {
- LinkedList<Integer> queue = new LinkedList<>(tickets);
- int res = 0;
- // pre-processing
- while (p >= 0) {
- int temp = queue.poll();
- if (temp > 1) {
- queue.offer(temp - 1);
- }
- if (temp == 1 && p == 0) {
- return res + 1;
- }
- p--;
- res++;
- }
- int target = queue.getLast();
- for (int cur : queue) {
- res += Math.min(cur, target);
- }
- return res;
-
- }
-
- }
复制代码
|