0%

Leetcode 1450. Number of Students Doing Homework at a Given Time

題目

1
2
3
4
5
6
7
Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
Output: 1
Explanation: We have 3 students where:
The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.
The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.
The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.

解法思維

做出endTimestartTime的陣列在查找

1
2
3
4
5
6
7
8
9
10
11
12
var busyStudent = function(startTime, endTime, queryTime) {
let sum=0
for (let j = 0; j < startTime.length; j++) {
let len=endTime[j]-startTime[j]
if(len==0){
Array.from({length:1},(v,i)=>endTime[j]).indexOf(queryTime)!=-1?sum++:false;
}else{
Array.from({length:len+1}, (v, i) => startTime[j]++).indexOf(queryTime)!=-1?sum++:false;
}
}
return sum
};

如果startTime小於目標時間和endTime大於目標時間,就代表目標時間包含在裡面

1
2
3
4
5
6
7
const busyStudent = (startTime, endTime, queryTime) => {
let ret = 0;
for (let i = 0; i < startTime.length; ++i) {
startTime[i] <= queryTime && endTime[i] >= queryTime && ++ret;
}
return ret;
};