mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
32 lines
506 B
Java
32 lines
506 B
Java
|
class Solution {
|
||
|
private long getSum(int[] chalk, int k) {
|
||
|
long sum = 0;
|
||
|
|
||
|
for (int uses : chalk) {
|
||
|
sum += uses;
|
||
|
if (sum > k) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return sum;
|
||
|
}
|
||
|
|
||
|
public int chalkReplacer(int[] chalk, int k) {
|
||
|
var partialSum = getSum(chalk, k);
|
||
|
|
||
|
// Remove whole iterations over the students
|
||
|
k %= partialSum;
|
||
|
|
||
|
for (int i = 0; i < chalk.length; ++i) {
|
||
|
if (k < chalk[i]) {
|
||
|
return i;
|
||
|
}
|
||
|
|
||
|
k -= chalk[i];
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
}
|