Skip to content

Commit 5d54dbb

Browse files
committedApr 3, 2025
Add solution #1094
1 parent b09d359 commit 5d54dbb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,132 LeetCode solutions in JavaScript
1+
# 1,133 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -874,6 +874,7 @@
874874
1091|[Shortest Path in Binary Matrix](./solutions/1091-shortest-path-in-binary-matrix.js)|Medium|
875875
1092|[Shortest Common Supersequence](./solutions/1092-shortest-common-supersequence.js)|Hard|
876876
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|
877878
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
878879
1108|[Defanging an IP Address](./solutions/1108-defanging-an-ip-address.js)|Easy|
879880
1122|[Relative Sort Array](./solutions/1122-relative-sort-array.js)|Easy|

‎solutions/1094-car-pooling.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.