Ctrl K

Who Refills the Coffee Beans

Medium
There are 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.
Example 1
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.

Example 2
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.

Constraints
  • 1 ≤ n = len(needs) ≤ 10⁵
  • 1 ≤ needs[i] ≤ 10⁵
  • 1 ≤ supply ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumWindows With a Small Product MediumLongest Run of Ones With k Fixes MediumShuttle Seat Plan MediumLargest Light Square Medium
loading Python…
Input
needs=[4, 2, 3], supply=20
Expected
0

Run checks the first 3 tests in your browser. Submit checks all 68 tests on the server, hidden ones included.