DDSA Solutions

1450. Number of Students Doing Homework at a Given Time

Time: O(n)
Space: O(1)
Advertisement

Intuition

Count students whose start time <= queryTime and end time >= queryTime.

Algorithm

  1. 1For each student i: if startTime[i] <= queryTime && endTime[i] >= queryTime: count++.

Common Pitfalls

  • Simple O(n) scan. Both conditions must hold.
1450.cs
C#
// Approach: Simple range check; count students whose [startTime, endTime] interval includes queryTime.
// Time: O(n) Space: O(1)

public class Solution
{
    public int BusyStudent(int[] startTime, int[] endTime, int queryTime)
    {
        int n = startTime.Length;
        int ans = 0;

        for (int i = 0; i < n; ++i)
        {
            if (startTime[i] <= queryTime && queryTime <= endTime[i])
                ++ans;
        }

        return ans;
    }
}
Advertisement
Was this solution helpful?