-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path1094-car-pooling.js
61 lines (56 loc) · 1.17 KB
/
1094-car-pooling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @param {number[][]} trips
* @param {number} capacity
* @return {boolean}
*/
const carPooling = function(trips, capacity) {
const arr = Array(1001).fill(0)
for(const [num, s, e] of trips) {
arr[s] += num
arr[e] -= num
}
for(let i = 1; i < 1001; i++) {
arr[i] += arr[i - 1]
}
for(let e of arr) {
if(e > capacity) return false
}
return true
};
// another
/**
* @param {number[][]} trips
* @param {number} capacity
* @return {boolean}
*/
const carPooling = function(trips, capacity) {
let stops = new Array(1001).fill(0)
for (let t of trips) {
stops[t[1]] += t[0]
stops[t[2]] -= t[0]
}
for (let i = 0; capacity >= 0 && i < 1001; ++i) capacity -= stops[i]
return capacity >= 0
}
// another
/**
* @param {number[][]} trips
* @param {number} capacity
* @return {boolean}
*/
const carPooling = function(trips, capacity) {
const arr = Array(1001).fill(0)
for(let el of trips) {
const [num, s, e] = el
arr[s] += num
arr[e] -= num
}
for(let i = 1; i < 1001; i++) {
if(arr[i] !== 0) arr[i] += arr[i - 1]
else arr[i] = arr[i - 1]
}
for(let e of arr) {
if(e > capacity) return false
}
return true
};