We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent db64a15 commit 260ba58Copy full SHA for 260ba58
javascript/1094-car-pooling.js
@@ -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