There are n
students in a class numbered from 0
to n - 1
. The teacher will give each student a problem starting with the student number 0
, then the student number 1
, and so on until the teacher reaches the student number n - 1
. After that, the teacher will restart the process, starting with the student number 0
again.
You are given a 0-indexed integer array chalk
and an integer k
. There are initially k
pieces of chalk. When the student number i
is given a problem to solve, they will use chalk[i]
pieces of chalk to solve that problem. However, if the current number of chalk pieces is strictly less than chalk[i]
, then the student number i
will be asked to replace the chalk.
Return the index of the student that will replace the chalk pieces.
Example 1:
Input: chalk = [5,1,5], k = 22 Output: 0 Explanation: The students go in turns as follows: - Student number 0 uses 5 chalk, so k = 17. - Student number 1 uses 1 chalk, so k = 16. - Student number 2 uses 5 chalk, so k = 11. - Student number 0 uses 5 chalk, so k = 6. - Student number 1 uses 1 chalk, so k = 5. - Student number 2 uses 5 chalk, so k = 0. Student number 0 does not have enough chalk, so they will have to replace it.
Example 2:
Input: chalk = [3,4,1,2], k = 25 Output: 1 Explanation: The students go in turns as follows: - Student number 0 uses 3 chalk so k = 22. - Student number 1 uses 4 chalk so k = 18. - Student number 2 uses 1 chalk so k = 17. - Student number 3 uses 2 chalk so k = 15. - Student number 0 uses 3 chalk so k = 12. - Student number 1 uses 4 chalk so k = 8. - Student number 2 uses 1 chalk so k = 7. - Student number 3 uses 2 chalk so k = 5. - Student number 0 uses 3 chalk so k = 2. Student number 1 does not have enough chalk, so they will have to replace it.
Constraints:
chalk.length == n
1 <= n <= 105
1 <= chalk[i] <= 105
1 <= k <= 109
This problem involves determining which student will need to replace the chalk given a number of chalk pieces and the chalk consumption of each student in a cyclical pattern.
The solution employs a two-step approach:
Calculate the total chalk consumption per cycle: Sum the chalk consumption of all students (chalk
array). Then, find the remainder when the initial chalk count (k
) is divided by the total chalk consumption. This remainder represents the remaining chalk after completing several full cycles.
Simulate the remaining cycle: Iterate through the students, simulating chalk usage. If at any point the remaining chalk (k
) is less than the current student's chalk consumption, that student needs to replace the chalk and their index is returned.
Time Complexity: O(n), where n is the number of students. In the worst case, we iterate through the students once to calculate the sum and then iterate through them again (potentially) in the simulation. The modulo operation is O(1).
Space Complexity: O(1). The algorithm uses only a few variables to store intermediate values; it doesn't scale with the input size.
class Solution:
def chalkReplacer(self, chalk: List[int], k: int) -> int:
s = sum(chalk) # Total chalk consumption per cycle
k %= s # Remaining chalk after full cycles
for i, x in enumerate(chalk): # Iterate through students
if k < x: # Check if current student needs to replace
return i
k -= x # Update remaining chalk if sufficient
class Solution {
public int chalkReplacer(int[] chalk, int k) {
long s = 0; // Use long to handle potential overflow
for (int x : chalk) {
s += x;
}
k %= s;
for (int i = 0; ; ++i) { // Iterate through students
if (k < chalk[i]) {
return i;
}
k -= chalk[i];
}
}
}
class Solution {
public:
int chalkReplacer(vector<int>& chalk, int k) {
long long s = accumulate(chalk.begin(), chalk.end(), 0LL); // Use long long to avoid overflow
k %= s;
for (int i = 0; ; ++i) {
if (k < chalk[i]) {
return i;
}
k -= chalk[i];
}
}
};
The implementations in other languages (Go, TypeScript, Rust) follow similar logic, adapting the syntax to each language's specifics while maintaining the same fundamental algorithm. The key elements—calculating the total chalk consumption per cycle using sum()
or accumulate()
, using the modulo operator (%
), and then iterating and simulating chalk usage—remain consistent across all language implementations.