File tree 2 files changed +40
-1
lines changed
2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,132 LeetCode solutions in JavaScript
1
+ # 1,133 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
874
874
1091|[ Shortest Path in Binary Matrix] ( ./solutions/1091-shortest-path-in-binary-matrix.js ) |Medium|
875
875
1092|[ Shortest Common Supersequence] ( ./solutions/1092-shortest-common-supersequence.js ) |Hard|
876
876
1093|[ Statistics from a Large Sample] ( ./solutions/1093-statistics-from-a-large-sample.js ) |Medium|
877
+ 1094|[ Car Pooling] ( ./solutions/1094-car-pooling.js ) |Medium|
877
878
1103|[ Distribute Candies to People] ( ./solutions/1103-distribute-candies-to-people.js ) |Easy|
878
879
1108|[ Defanging an IP Address] ( ./solutions/1108-defanging-an-ip-address.js ) |Easy|
879
880
1122|[ Relative Sort Array] ( ./solutions/1122-relative-sort-array.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1094. Car Pooling
3
+ * https://leetcode.com/problems/car-pooling/
4
+ * Difficulty: Medium
5
+ *
6
+ * There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn
7
+ * around and drive west).
8
+ *
9
+ * You are given the integer capacity and an array trips where
10
+ * trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi
11
+ * passengers and the locations to pick them up and drop them off are fromi and toi respectively.
12
+ * The locations are given as the number of kilometers due east from the car's initial location.
13
+ *
14
+ * Return true if it is possible to pick up and drop off all passengers for all the given trips,
15
+ * or false otherwise.
16
+ */
17
+
18
+ /**
19
+ * @param {number[][] } trips
20
+ * @param {number } capacity
21
+ * @return {boolean }
22
+ */
23
+ var carPooling = function ( trips , capacity ) {
24
+ const timeline = new Array ( 1001 ) . fill ( 0 ) ;
25
+
26
+ for ( const [ passengers , start , end ] of trips ) {
27
+ timeline [ start ] += passengers ;
28
+ timeline [ end ] -= passengers ;
29
+ }
30
+
31
+ let currentPassengers = 0 ;
32
+ for ( const change of timeline ) {
33
+ currentPassengers += change ;
34
+ if ( currentPassengers > capacity ) return false ;
35
+ }
36
+
37
+ return true ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments