-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe-supermarket-queue.java
41 lines (29 loc) · 1.01 KB
/
the-supermarket-queue.java
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
import java.util.Arrays;
import java.util.*;
public class Solution {
public static int solveSuperMarketQueue(int[] customers, int n) {
if (customers.length == 0) {
return 0;
}
if (n <= 0) {
return -1; // Invalid input
}
PriorityQueue<Integer> tills = new PriorityQueue<>(n);
// Initialize the priority queue with the first n customers
for (int i = 0; i < n && i < customers.length; i++) {
tills.offer(customers[i]);
}
// Process the remaining customers
for (int i = n; i < customers.length; i++) {
int minTimeTill = tills.poll();
minTimeTill += customers[i];
tills.offer(minTimeTill);
}
// Find the maximum total time among tills
int maxTime = Integer.MIN_VALUE;
for (int time : tills) {
maxTime = Math.max(maxTime, time);
}
return maxTime;
}
}