Find the Student that Will Replace the Chalk (LeetCode 1894): Solution and Explanation
Students take turns in a loop, and student i uses chalk[i] pieces per turn. Starting with k pieces, return the index of the first student who needs more chalk than is left. For chalk = [5,1,5] and k = 22 the answer is 0.
Intuition
Simulating turn by turn can take up to k / min(chalk) steps, which is far too many when k is 10^9 and every student uses 1 piece.
A full round of the class always uses the same amount, total = sum(chalk). Removing whole rounds never changes who runs out, so only k % total pieces matter, and that is less than one round.
Inside a single round the chalk used so far only grows, so the prefix sums are sorted. The student who runs out is the first index whose prefix sum is greater than the remainder, which is a classic binary search.
Approach: Remainder plus binary search on prefix sums
- Build
prefix[i] = chalk[0] + ... + chalk[i]with a 64-bit running total. The last entry istotal. - Replace
kwithk % total, the chalk left after all complete rounds. - Binary search with
lo = 0andhi = n - 1. Ifprefix[mid] > k, studentmidcannot finish, so sethi = mid. Otherwise everyone up tomidfinishes, so setlo = mid + 1. - When
lo == hi, returnlo.
Step-by-step example
chalk = [5,1,5], k = 22
- Prefix sums are [5, 6, 11], so one round costs 11.
- 22 % 11 = 0: two full rounds use all 22 pieces and 0 are left.
- lo = 0, hi = 2. mid = 1: prefix[1] = 6 > 0, so hi = 1.
- mid = 0: prefix[0] = 5 > 0, so hi = 0.
- lo = hi = 0. Student 0 needs 5 pieces with 0 left, so the answer is 0.
Code: Python, Java, C++
1class Solution:
2 def chalkReplacer(self, chalk: List[int], k: int) -> int:
3 prefix = []
4 total = 0
5 for c in chalk:
6 total += c
7 prefix.append(total)
8 k %= total
9 lo, hi = 0, len(chalk) - 1
10 while lo < hi:
11 mid = (lo + hi) // 2
12 if prefix[mid] > k:
13 hi = mid
14 else:
15 lo = mid + 1
16 return lo1class Solution {
2 public int chalkReplacer(int[] chalk, int k) {
3 int n = chalk.length;
4 long[] prefix = new long[n];
5 long total = 0;
6 for (int i = 0; i < n; i++) {
7 total += chalk[i];
8 prefix[i] = total;
9 }
10 long rest = k % total;
11 int lo = 0, hi = n - 1;
12 while (lo < hi) {
13 int mid = (lo + hi) / 2;
14 if (prefix[mid] > rest) {
15 hi = mid;
16 } else {
17 lo = mid + 1;
18 }
19 }
20 return lo;
21 }
22}1class Solution {
2public:
3 int chalkReplacer(vector<int>& chalk, int k) {
4 int n = chalk.size();
5 vector<long long> prefix(n);
6 long long total = 0;
7 for (int i = 0; i < n; i++) {
8 total += chalk[i];
9 prefix[i] = total;
10 }
11 long long rest = k % total;
12 int lo = 0, hi = n - 1;
13 while (lo < hi) {
14 int mid = (lo + hi) / 2;
15 if (prefix[mid] > rest) {
16 hi = mid;
17 } else {
18 lo = mid + 1;
19 }
20 }
21 return lo;
22 }
23};Time and space complexity
O(n)
Building the prefix sums is O(n). The binary search adds O(log n).
O(n)
The prefix array holds n running totals. A second linear pass instead of the search would bring it to O(1).
Other approaches
Remainder plus linear scan (time O(n), space O(1))
After k %= total, walk the students and subtract chalk[i] until it is larger than what is left. Same O(n) time and O(1) extra space.
Direct simulation (time O(k / min(chalk)), space O(1))
Loop over the class subtracting chalk until someone cannot be served. It can run about 10^9 iterations when every student uses 1 piece.
Common mistakes
- Summing in 32-bit. With 10^5 students using up to 10^5 pieces each, the total reaches 10^10 and overflows a 32-bit int.
- Using
>=instead of>. A student whose need exactly equals the chalk left can still finish, so the search looks for the first prefix strictly greater than the remainder. - Skipping the modulo and binary searching on
kitself. Whenkis larger than one round, no prefix exceeds it and the search returns the wrong student.
Frequently asked questions
Why is it safe to throw away full rounds?
Each full round uses exactly total pieces and ends back at student 0 with nobody running out, because k was at least total before the round. So the situation after removing a round is the same problem with a smaller k.
Why are the prefix sums sorted?
Every chalk[i] is at least 1, so each running total is strictly larger than the one before. That monotonic order is what makes binary search valid.
Can the remainder be 0?
Yes. Then nobody can afford a turn and student 0 replaces the chalk. The search handles it because prefix[0] is at least 1, which is greater than 0.