n colleagues in an office taking turns at the coffee machine: colleague 0 goes first, then 1, and so on up to n - 1, after which the queue starts over from colleague 0. Colleague i needs needs[i] grams of beans for one cup.
The machine starts with supply grams. As soon as the colleague at the machine finds fewer than needs[i] grams left, they have to go and refill it.
Return the index of the colleague who ends up refilling the beans.needs=[4, 2, 3], supply=20 → 0
A full round uses 9 grams. After two rounds 2 grams are left, and colleague 0 needs 4, so they refill.
needs=[1, 5, 2], supply=7 → 2
Colleague 0 takes 1 gram (6 left), colleague 1 takes 5 (1 left), colleague 2 needs 2, which is not enough.
needs=[4, 2, 3], supply=20
0
Run checks the first 3 tests in your browser. Submit checks all 68 tests on the server, hidden ones included.