There are N clients who have ordered N handmade items. The K-th client ordered exactly one item that takes T[k] hours to make. There is only one employee who makes items for clients, and they work in the following manner:
For ex, for T = [3, 1, 2], the employee spends 6 hours making items in the following order:[1,2,3,1,3,1]. The first client waited 6 hours for item, the 2nd waited for 2 hours and the 3rd 5 hours. The total hours is 6 + 2+ 5 = 13
return the answer mod by 10^9
ex: [3, 1, 2]: 13
[1, 2, 3, 4] order sequence: 1,2,3,4,2,3,4,3,4,4, each waited 1, 5, 8, 10 hours which in sum is 24
[7,7,7]: 60
[10000]: 10000
I used a queue to simulate the process. Instead of recording time for each person, I record the total wait time for all. For ex, if there is 3 persons in queue, all have to wait 3 hous in total with each waiting 1 hour. Is there any better idea? Thanks
private int time(int[] T) {
if (T == null || T.length == 0) {
return 0;
}
int n = T.length;
if (n == 1) {
return T[0];
}
int mod = 100000000;
Queue<Integer> queue = new ArrayDeque<>();
for (int x : T) {
queue.offer(x);
}
long time = 0;
while (queue.size() > 1) {
time = (time + queue.size()) % mod;
int cur = queue.poll();
cur--;
if (cur > 0) {
queue.offer(cur);
}
}
time = (time + queue.poll()) % mod;
return (int) time;
}