Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1450. Number of Students Doing Homework at a Given Time - Leetcode Solution

Code Implementation

class Solution:
    def busyStudent(self, startTime, endTime, queryTime):
        count = 0
        for s, e in zip(startTime, endTime):
            if s <= queryTime <= e:
                count += 1
        return count
      
class Solution {
public:
    int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
        int count = 0;
        for (int i = 0; i < startTime.size(); ++i) {
            if (startTime[i] <= queryTime && queryTime <= endTime[i]) {
                ++count;
            }
        }
        return count;
    }
};
      
class Solution {
    public int busyStudent(int[] startTime, int[] endTime, int queryTime) {
        int count = 0;
        for (int i = 0; i < startTime.length; i++) {
            if (startTime[i] <= queryTime && queryTime <= endTime[i]) {
                count++;
            }
        }
        return count;
    }
}
      
var busyStudent = function(startTime, endTime, queryTime) {
    let count = 0;
    for (let i = 0; i < startTime.length; i++) {
        if (startTime[i] <= queryTime && queryTime <= endTime[i]) {
            count++;
        }
    }
    return count;
};
      

Problem Description

Given two integer arrays startTime and endTime representing the start and end times of homework for each student, and an integer queryTime, determine how many students are doing homework at queryTime.

  • Each student starts their homework at startTime[i] and finishes at endTime[i] (inclusive).
  • For each student, the time interval is closed: startTime[i] ≤ t ≤ endTime[i].
  • You must count the number of students for whom queryTime falls within their homework interval.
  • Assume all arrays are of the same length and contain valid integer values.

Thought Process

The problem asks us to count how many students are busy doing homework at a specific time. For each student, we know the time they start and finish their homework. To solve this, we need to check, for each student, whether the queryTime falls within their homework interval.

The most direct way is to go through each student's interval and see if queryTime is between startTime[i] and endTime[i] (inclusive). If it is, we increment our count.

Since the number of students is likely not huge (given typical constraints), this straightforward approach is both simple and efficient.

Solution Approach

Let's break down the algorithm step by step:

  1. Initialize a counter: Start with a variable (e.g., count) set to zero. This will keep track of the number of students doing homework at queryTime.
  2. Iterate through each student: For each index i, check if queryTime is within the interval [startTime[i], endTime[i]].
  3. Check the condition: If startTime[i] ≤ queryTime ≤ endTime[i], increment count by one.
  4. Return the result: After checking all students, return count as the answer.

We use a simple loop because each check is O(1), and there are no overlapping or complex interval queries that would require more advanced data structures.

Example Walkthrough

Example:

  • startTime = [1, 2, 3]
  • endTime = [3, 2, 7]
  • queryTime = 4
  1. Student 0: startTime[0] = 1, endTime[0] = 3. Is 4 between 1 and 3? No.
  2. Student 1: startTime[1] = 2, endTime[1] = 2. Is 4 between 2 and 2? No.
  3. Student 2: startTime[2] = 3, endTime[2] = 7. Is 4 between 3 and 7? Yes. Increment count to 1.

After checking all students, only one student is doing homework at time 4. The function should return 1.

Time and Space Complexity

  • Brute-force approach: The naive solution is to check for every student if queryTime is within their interval. This is O(n), where n is the number of students.
  • Optimized approach: The solution above is already optimal for this problem, as each check is independent and requires only O(1) time.
  • Space complexity: O(1), since we only use a single counter regardless of input size.

There is no need for extra data structures or pre-processing, as the problem is simple and direct.

Summary

To determine how many students are doing homework at a given time, we simply iterate through each student's start and end times and count those whose interval includes the queryTime. The solution is straightforward, efficient, and does not require any advanced techniques or data structures. This makes it an elegant example of a simple scanning problem where a direct approach is both effective and optimal.