Number of Students Doing Homework at a Given Time

Below is the Python code to find the number of students doing their homework at the given queryTime, considering the startTime and endTime for each student.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution:
    def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
        # Initialize the count of students doing homework at queryTime
        count = 0

        # Iterate through the start and end times of each student
        for i in range(len(startTime)):
            start = startTime[i]
            end = endTime[i]

            # Check if queryTime is within the start and end time of the student's homework
            if start <= queryTime and queryTime <= end:
                count += 1

        return count

Explanation:

  1. Initialize Count: We start by initializing a count variable to keep track of the number of students doing homework at queryTime.
  2. Iterate Through Students: We loop through the startTime and endTime arrays to examine each student’s homework time.
  3. Check Time: For each student, we check if queryTime falls within their homework interval [startTime[i], endTime[i]]. If it does, we increment the count.
  4. Return Result: Finally, we return the count, representing the number of students doing their homework at queryTime.