-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_2073.java
More file actions
48 lines (42 loc) · 1.3 KB
/
_2073.java
File metadata and controls
48 lines (42 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package leetcode;
import java.util.*;
import java.util.stream.Collectors;
class Solution {
public int timeRequiredToBuy(int[] tickets, int k) {
List<Integer> list = Arrays.stream(tickets).boxed().collect(Collectors.toList());
Deque<Integer> queue = new ArrayDeque<>(list);
int num = queue.pollFirst() - 1;
int time = 1;
while (!(num == 0 && k == 0)) {
if (num != 0) {
queue.addLast(num);
}
if (k == 0) {
k = queue.size() - 1;
} else {
k--;
}
num = queue.pollFirst() - 1;
time++;
}
return time;
}
public int timeRequiredToBuy2(int[] tickets, int k) {
int time = 0;
for (int i = 0; i < tickets.length; i++) {
if (i <= k) {
time += Math.min(tickets[i], tickets[k]);
} else {
time += Math.min(tickets[i], tickets[k] - 1);
}
}
return time;
}
}
public class _2073 {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.timeRequiredToBuy2(new int[] {2, 3, 2}, 2));
System.out.println(solution.timeRequiredToBuy2(new int[] {5, 1, 1, 1}, 0));
}
}