Find the Student that Will Replace the Chalk - Leet Code 1894 - Python - in three simple steps
Shojib
1 min read
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)
Video Explanation
0
Subscribe to my newsletter
Read articles from Shojib directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shojib
Shojib
Hey, I'm Kamruzaman Shojib and I'm a front-end developer. I love to talk about tech mostly I'll be writing about JavaScript and trying to help the community.