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;
};
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
.
startTime[i]
and finishes at endTime[i]
(inclusive).startTime[i] ≤ t ≤ endTime[i]
.queryTime
falls within their homework interval.
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.
Let's break down the algorithm step by step:
count
) set to zero. This will keep track of the number of students doing homework at queryTime
.
i
, check if queryTime
is within the interval [startTime[i], endTime[i]]
.
startTime[i] ≤ queryTime ≤ endTime[i]
, increment count
by one.
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:
startTime = [1, 2, 3]
endTime = [3, 2, 7]
queryTime = 4
startTime[0] = 1
, endTime[0] = 3
. Is 4
between 1 and 3? No.
startTime[1] = 2
, endTime[1] = 2
. Is 4
between 2 and 2? No.
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.
queryTime
is within their interval. This is O(n), where n is the number of students.
There is no need for extra data structures or pre-processing, as the problem is simple and direct.
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.