Intuition¶
When given the problem, the first thought is that the large value of k may exceed the total amount of chalk available across all students. Therefore, it's intuitive to reduce k by taking the modulo of the sum of all chalk, as this would give us the effective amount of chalk k remaining after potentially multiple full rounds of chalk usage.
Approach¶
- Calculate Total Chalk: First, sum up the total amount of chalk that would be used in one complete round.
- Reduce k: Take
k % totalto reducekto a value that represents the remaining chalk after several full rounds. - Determine the Student: Iterate through the array of chalk. Subtract each student's chalk usage from
kuntilkbecomes less than the chalk needed by a student, which indicates that the current student will be the one who runs out of chalk.
Complexity¶
-
Time complexity:
-
$O(n)$, where
nis the number of students. This is because we traverse the array twice, once to calculate the total chalk and once to find the student. -
Space complexity:
- $O(1)$, as no extra space is used other than a few variables.
Code¶
Java¶
class Solution {
public int chalkReplacer(int[] chalk, int k) {
long total = 0;
for (int c : chalk) {
total += c;
}
k %= total;
for (int i = 0; i < chalk.length; i++) {
if (k < chalk[i]) {
return i;
}
k -= chalk[i];
}
return 0;
}
}
Rust¶
impl Solution {
pub fn chalk_replacer(chalk: Vec<i32>, k: i32) -> i32 {
let sum:usize = chalk.iter().fold(0usize, |acc, &num| acc + (num as usize));
let mut k = (k as usize) % sum;
for i in 0..chalk.len() {
if k < chalk[i] as usize {
return i as i32;
}
k-= chalk[i] as usize;
}
0
}
}