Leetcode 1450. Number of Students Doing Homework at a Given Time
You are given two integer arrays startTime
and endTime
and an integer queryTime
.
The i-th
student started doing their homework at the time startTime[i]
and finished it at time endTime[i]
.
Return the number of students doing their homework at the queryTime
. More formally, return the number of students where queryTime
lays in the interval [startTime[i], endTime[i]]
inclusive.
startTime = [1,2,3]
endTime = [3,2,7]
queryTime = 4
1
4
.startTime = [4]
endTime = [4]
queryTime = 4
1
4
.startTime.length == endTime.length
1 <= startTime.length <= 100
1 <= startTime[i] <= endTime[i] <= 1000
1 <= queryTime <= 1000
queryTime
?
0
if no students are found doing homework at queryTime
.queryTime
.queryTime
lies within the interval [startTime[i], endTime[i]]
.#include <vector>
int busyStudent(std::vector<int>& startTime, std::vector<int>& endTime, int queryTime) {
int count = 0;
int n = startTime.size();
for (int i = 0; i < n; ++i) {
if (startTime[i] <= queryTime && queryTime <= endTime[i]) {
count++;
}
}
return count;
}
count
to 0: This will keep track of the number of students working at queryTime
.i
, check if startTime[i]
is less than or equal to queryTime
and endTime[i]
is greater than or equal to queryTime
.queryTime
, so increment the count
.count
: After checking all students, return the final count.n
is the length of the startTime
or endTime
array.startTime.length
can be at most 100.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?