Find the Student that Will Replace the Chalk - Leet Code 1894 - Python - in three simple steps
Hey everyone, Today we will solve Find the Student that Will Replace the Chalk - Leet Code 1894 in three simple steps.
Calculate the
total chalk
needed for one full roundReduce
k
modulo total_chalkIterate through the array to find the first student who can't complete their turn
def chalkReplacer(self, chalk: List[int], k: int) -> int:
# Step 1: Calculate the total chalk needed for one full round
total_chalk = sum(chalk)
# Step 2: Reduce k modulo total_chalk
k %= total_chalk
# iterate though array who can't find their turn
for i in range(len(chalk)):
if k < chalk[i]:
return i
# decrement k by chalk[i]
k-=chalk[i]
Complexity
Time complexity:
O(n), wheren
is the number of students.Space complexity: O(1)