diff options
author | Jack Sangdahl <[email protected]> | 2024-09-01 20:52:22 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-09-01 20:52:22 -0600 |
commit | 179f111f17489e2fe1a0d16f9fdb8d85b509bed2 (patch) | |
tree | 30140e73e06981fe3f849e0dc56e23abf9090184 | |
parent | 9af6162fb65972b6a01bad4ed6fec20d38195b6b (diff) | |
download | leetcode-179f111f17489e2fe1a0d16f9fdb8d85b509bed2.tar.gz leetcode-179f111f17489e2fe1a0d16f9fdb8d85b509bed2.zip |
1894. find the student that will replace the chalk
-rw-r--r-- | find_student_replace_chalk.c | 35 | ||||
-rw-r--r-- | find_student_replace_chalk.py | 36 |
2 files changed, 71 insertions, 0 deletions
diff --git a/find_student_replace_chalk.c b/find_student_replace_chalk.c new file mode 100644 index 0000000..da7bcd5 --- /dev/null +++ b/find_student_replace_chalk.c @@ -0,0 +1,35 @@ +// 1894. Find the Student that Will Replace the Chalk +#include "leetcode.h" + +/* + * 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 teach reaches the student number 'n - + * 1'. after that the teach will restart the processes, starting with the + * student number 0 again. 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. return the index of the student that will replace the + * chalk pieces. + */ + +int chalkReplacer(int *chalk, int chalkSize, int k) { + int sum = 0; + for (int i = 0; i < chalkSize; i++) + sum += chalk[i]; + int rem = k % sum; + for (int i = 0; i < chalkSize; i++) { + rem -= chalk[i]; + if (rem < 0) + return i; + } + return -1; +} + +int main() { + int c1[] = {5, 1, 5}, c2[] = {3, 4, 1, 2}; + printf("%d\n", chalkReplacer(c1, ARRAY_SIZE(c1), 22)); // expect: 0 + printf("%d\n", chalkReplacer(c2, ARRAY_SIZE(c2), 25)); // expect: 1 +} diff --git a/find_student_replace_chalk.py b/find_student_replace_chalk.py new file mode 100644 index 0000000..2e040f0 --- /dev/null +++ b/find_student_replace_chalk.py @@ -0,0 +1,36 @@ +# 1894. Find the Student that Will Replace the Chalk + +""" +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 teach reaches the student number 'n - +1'. after that the teach will restart the processes, starting with the +student number 0 again. 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. return the index of the student that will replace the +chalk pieces. +""" + + +class Solution(object): + def chalkReplacer(self, chalk, k): + """ + :type chalk: List[int] + :type k: int + :rtype: int + """ + k %= sum(chalk) + for i, j in enumerate(chalk): + if k < j: + return i + k -= j + return 0 + + +if __name__ == "__main__": + obj = Solution() + print(obj.chalkReplacer(chalk=[5, 1, 5], k=22)) + print(obj.chalkReplacer(chalk=[3, 4, 1, 2], k=25)) |