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 teacher reaches the student number n - 1. After that, the teacher will restart the process, 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.
Example 1:
Input: chalk = [5,1,5], k = 22 Output: 0 Explanation: The students go in turns as follows: - Student number 0 uses 5 chalk, so k = 17. - Student number 1 uses 1 chalk, so k = 16. - Student number 2 uses 5 chalk, so k = 11. - Student number 0 uses 5 chalk, so k = 6. - Student number 1 uses 1 chalk, so k = 5. - Student number 2 uses 5 chalk, so k = 0. Student number 0 does not have enough chalk, so they will have to replace it.
Example 2:
Input: chalk = [3,4,1,2], k = 25 Output: 1 Explanation: The students go in turns as follows: - Student number 0 uses 3 chalk so k = 22. - Student number 1 uses 4 chalk so k = 18. - Student number 2 uses 1 chalk so k = 17. - Student number 3 uses 2 chalk so k = 15. - Student number 0 uses 3 chalk so k = 12. - Student number 1 uses 4 chalk so k = 8. - Student number 2 uses 1 chalk so k = 7. - Student number 3 uses 2 chalk so k = 5. - Student number 0 uses 3 chalk so k = 2. Student number 1 does not have enough chalk, so they will have to replace it.
Constraints:
chalk.length == n1 <= n <= 1051 <= chalk[i] <= 1051 <= k <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're figuring out which student will run out of chalk first by simulating the entire chalk distribution process step-by-step. This means we'll go through each student one by one, subtracting the chalk they need until we find the student who can't complete their task.
Here's how the algorithm would work step-by-step:
def find_student_replacing_chalk_brute_force(
chalk_needed_by_each_student,
total_chalk_available
):
number_of_students = len(chalk_needed_by_each_student)
for student_index in range(number_of_students):
# Subtract the chalk needed by current student
total_chalk_available -= \
chalk_needed_by_each_student[student_index]
# If total chalk becomes negative, current student
# is the one who needs to replace
if total_chalk_available < 0:
return student_index
# This should never happen given problem constraints.
return 0We want to find the student who will run out of chalk during a repetitive process. Instead of simulating the entire process, we can use math to significantly speed things up. This involves figuring out the total chalk needed for many rounds and using the remainder to determine the final student.
Here's how the algorithm would work step-by-step:
def find_student_to_replace_chalk(chalk_needed, chalk_available):
total_chalk_needed_for_cycle = sum(chalk_needed)
# Optimize by completing full cycles if possible.
complete_cycles = chalk_available // total_chalk_needed_for_cycle
remaining_chalk = chalk_available - (complete_cycles * total_chalk_needed_for_cycle)
for student_index, chalk_required in enumerate(chalk_needed):
# Subtract each student's chalk usage from remaining.
remaining_chalk -= chalk_required
if remaining_chalk < 0:
# The student that makes chalk negative is the one.
return student_index
| Case | How to Handle |
|---|---|
| Empty chalk array | Return -1 or throw an exception, indicating invalid input. |
| k is negative | Return -1 or throw an exception, indicating invalid input. |
| chalk array contains zero values | The algorithm should handle zeros correctly; a student with zero chalk requirement will always pass the turn until k is reduced enough for another student's chalk amount. |
| Sum of chalk array is less than or equal to k | Reduce k modulo the sum of the chalk array to effectively simulate multiple cycles of chalk usage before finding the student that replaces the chalk. |
| chalk array contains very large integer values that might cause integer overflow during summation | Use long data type to store the running sum of chalk or k to prevent integer overflow. |
| k is very large and close to the maximum integer value | Reducing k modulo the sum of the chalk ensures we don't iterate excessively. |
| chalk array has only one element and k is less than that element | Return index 0 immediately, as the only student will always replace the chalk. |
| All elements in chalk are equal | The solution will cycle through the array until k is less than one of the values, handled correctly in the iteration logic. |