1450. Number of Students Doing Homework at a Given Time
Explanation:
- Iterate through the startTime and endTime arrays simultaneously.
- For each student, check if the queryTime falls within the interval [startTime[i], endTime[i]].
- Increment a counter if the queryTime is within the interval.
- Return the final count as the number of students doing homework at the given queryTime.
Time Complexity: O(n) where n is the number of students
Space Complexity: O(1)
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;
}
}
Code Editor (Testing phase)
Improve Your Solution
Use the editor below to refine the provided solution. Select a programming language and try the following:
- Add import statement if required.
- Optimize the code for better time or space complexity.
- Add test cases to validate edge cases and common scenarios.
- Handle error conditions or invalid inputs gracefully.
- Experiment with alternative approaches to deepen your understanding.
Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.