mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
java: add «1894. Find the Student that Will Replace the Chalk»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
4e73b65849
commit
43d48f5fcc
1 changed files with 31 additions and 0 deletions
31
java/find-the-student-that-will-replace-the-chalk.java
Normal file
31
java/find-the-student-that-will-replace-the-chalk.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue