Question Details
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...
Full Details
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:
* spend one hour making the first item;
* if the iterm is finished, the employee delivers it to the client immediately
* if the iterm is not finished, they put it after the N-th item for further work
* the employeestarts making the next item.
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
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;
}
About This Question
This is a reported interview question from a microsoft interview for a swe role during the oa round reported in 2024.