Skip to content

Commit 260ba58

Browse files
authored
Create 1094-car-pooling.js
1 parent db64a15 commit 260ba58

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: javascript/1094-car-pooling.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* MinHeap
3+
* Time O(n*log(n)) | Space O(n)
4+
* https://leetcode.com/problems/car-pooling/
5+
* @param {number[][]} trips
6+
* @param {number} capacity
7+
* @return {boolean}
8+
*/
9+
var carPooling = function(trips, capacity) {
10+
11+
const minQ = new MinPriorityQueue({
12+
compare: (e1, e2) => {
13+
return e1[0] - e2[0];
14+
}
15+
});
16+
17+
trips.sort((a,b) => a[1]-b[1]);
18+
19+
for(let i = 0; i < trips.length; i++) {
20+
while(!minQ.isEmpty() && minQ.front()[0] <= trips[i][1]) {
21+
capacity += minQ.dequeue()[1];
22+
};
23+
24+
capacity -= trips[i][0];
25+
if(capacity < 0) return false;
26+
minQ.enqueue([trips[i][2], trips[i][0]]);
27+
}
28+
29+
return true;
30+
};

0 commit comments

Comments
 (0)