1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02: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:
Matej Focko 2024-09-02 22:22:20 +02:00
parent 4e73b65849
commit 43d48f5fcc
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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;
}
}