1894. Find the Student that Will Replace the Chalk Medium

@problem@discussion
#Array#Binary Search#Simulation#Prefix Sum



1/**
2 * [1894] Find the Student that Will Replace the Chalk
3 *
4 * 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.
5 * 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.
6 * Return the index of the student that will replace the chalk.
7 *  
8 * Example 1:
9 * 
10 * Input: chalk = [5,1,5], k = 22
11 * Output: 0
12 * Explanation: The students go in turns as follows:
13 * - Student number 0 uses 5 chalk, so k = 17.
14 * - Student number 1 uses 1 chalk, so k = 16.
15 * - Student number 2 uses 5 chalk, so k = 11.
16 * - Student number 0 uses 5 chalk, so k = 6.
17 * - Student number 1 uses 1 chalk, so k = 5.
18 * - Student number 2 uses 5 chalk, so k = 0.
19 * Student number 0 does not have enough chalk, so they will have to replace it.
20 * Example 2:
21 * 
22 * Input: chalk = [3,4,1,2], k = 25
23 * Output: 1
24 * Explanation: The students go in turns as follows:
25 * - Student number 0 uses 3 chalk so k = 22.
26 * - Student number 1 uses 4 chalk so k = 18.
27 * - Student number 2 uses 1 chalk so k = 17.
28 * - Student number 3 uses 2 chalk so k = 15.
29 * - Student number 0 uses 3 chalk so k = 12.
30 * - Student number 1 uses 4 chalk so k = 8.
31 * - Student number 2 uses 1 chalk so k = 7.
32 * - Student number 3 uses 2 chalk so k = 5.
33 * - Student number 0 uses 3 chalk so k = 2.
34 * Student number 1 does not have enough chalk, so they will have to replace it.
35 * 
36 *  
37 * Constraints:
38 * 
39 * 	chalk.length == n
40 * 	1 <= n <= 10^5
41 * 	1 <= chalk[i] <= 10^5
42 * 	1 <= k <= 10^9
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/
48// discuss: https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53    pub fn chalk_replacer(chalk: Vec<i32>, k: i32) -> i32 {
54        0
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_1894() {
66    }
67}
68


Back
© 2025 bowen.ge All Rights Reserved.