Member-only story

Leetcode 1450: Number of students doing homework at a given time

Pierre-Marie Poitevin
1 min readMay 25, 2020

--

In this Leetcode problem, w are given start and end working times for students, and should return how many students work at a given time queryTime.

Problem statement

Solution

For each student i, we know they work at queryTime if and only if:

startTime[i] <= queryTime <= endTime[i]

We test that condition for each student and return the sum. Here is the full code solution:

func busyStudent(startTime []int, endTime []int, queryTime int) int {
sum := 0
n := len(startTime)
for i := 0; i < n; i++ {
if queryTime >= startTime[i] && queryTime <= endTime[i] {
sum++
}
}
return sum
}

Happy coding :)

What’s next:

--

--

Pierre-Marie Poitevin
Pierre-Marie Poitevin

No responses yet