#1450

Number of Students Doing Homework at a Given Time

Easy
ArrayArrayInterval Checking
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n)
O(n)
Space
O(1)
O(1)
💡

Intuition

Time O(n)Space O(1)

The brute force approach is already efficient for the given constraints, but we can ensure clarity and maintainability by using a more structured approach with list comprehensions or functional programming.

⚙️

Algorithm

2 steps
  1. 1Step 1: Use a list comprehension or filter to count students whose queryTime falls within their start and end times.
  2. 2Step 2: Return the length of the resulting list.
solution.py2 lines
1def busyStudents(startTime, endTime, queryTime):
2    return sum(1 for s, e in zip(startTime, endTime) if s <= queryTime <= e)

Complexity note: The time complexity remains O(n) since we still iterate through the list of students once. The space complexity is O(1) as we do not use any additional data structures that grow with input size.

  • 1Understanding intervals is crucial; queryTime must be checked against both start and end times.
  • 2Using list comprehensions or functional programming can lead to cleaner and more maintainable code.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.