Member-only story
Leetcode 1450: Number of students doing homework at a given time
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
.
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:
- Checkout the solution for another problem: Minimum score triangulation of polygon
- Contact me: poitevinpm@gmail.com
- Looking to interview for a software engineer position? Contact me :)