题目链接
解题思路
- 初始化
ans = 0
- 遍历
startTime
和endTime
数组,当满足startTime[i] <= queryTime && queryTime <= endTime[i]
时,ans++
AC代码
class Solution {
public int busyStudent(int[] startTime, int[] endTime, int queryTime) {
int ans = 0;
for (int i = 0; i < startTime.length; i++) {
if (startTime[i] <= queryTime && queryTime <= endTime[i])
ans++;
}
return ans;
}
}
本地测试代码
package com.company;
public class Solution_1450 {
public static int busyStudent(int[] startTime, int[] endTime, int queryTime) {
int ans = 0;
for (int i = 0; i < startTime.length; i++) {
if (startTime[i] <= queryTime && queryTime <= endTime[i])
ans++;
}
return ans;
}
public static void main(String[] args) {
System.out.println(busyStudent(new int[]{1, 2, 3}, new int[]{3, 2, 7}, 4));
System.out.println(busyStudent(new int[]{4}, new int[]{4}, 4));
System.out.println(busyStudent(new int[]{4}, new int[]{4}, 5));
System.out.println(busyStudent(new int[]{1, 1, 1, 1}, new int[]{1, 3, 2, 4}, 7));
System.out.println(busyStudent(new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1}, new int[]{10, 10, 10, 10, 10, 10, 10, 10, 10}, 5));
}
}