Skip to content

Commit 294a031

Browse files
committed
1094
1 parent 239f213 commit 294a031

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

leetcode/1094. Car Pooling/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# [1094. Car Pooling (Medium)](https://leetcode.com/problems/car-pooling/)
2+
3+
<p>You are driving a vehicle that&nbsp;has <code>capacity</code> empty seats initially available for passengers.&nbsp; The vehicle <strong>only</strong> drives east (ie. it <strong>cannot</strong> turn around and drive west.)</p>
4+
5+
<p>Given a list of <code>trips</code>, <code>trip[i] = [num_passengers, start_location, end_location]</code>&nbsp;contains information about the <code>i</code>-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off.&nbsp; The locations are given as the number of kilometers&nbsp;due east from your vehicle's initial location.</p>
6+
7+
<p>Return <code>true</code> if and only if&nbsp;it is possible to pick up and drop off all passengers for all the given trips.&nbsp;</p>
8+
9+
<p>&nbsp;</p>
10+
11+
<p><strong>Example 1:</strong></p>
12+
13+
<pre><strong>Input: </strong>trips = <span id="example-input-1-1">[[2,1,5],[3,3,7]]</span>, capacity = <span id="example-input-1-2">4</span>
14+
<strong>Output: </strong><span id="example-output-1">false</span>
15+
</pre>
16+
17+
<div>
18+
<p><strong>Example 2:</strong></p>
19+
20+
<pre><strong>Input: </strong>trips = <span id="example-input-2-1">[[2,1,5],[3,3,7]]</span>, capacity = <span id="example-input-2-2">5</span>
21+
<strong>Output: </strong><span id="example-output-2">true</span>
22+
</pre>
23+
24+
<div>
25+
<p><strong>Example 3:</strong></p>
26+
27+
<pre><strong>Input: </strong>trips = <span id="example-input-3-1">[[2,1,5],[3,5,7]]</span>, capacity = <span id="example-input-3-2">3</span>
28+
<strong>Output: </strong><span id="example-output-3">true</span>
29+
</pre>
30+
31+
<div>
32+
<p><strong>Example 4:</strong></p>
33+
34+
<pre><strong>Input: </strong>trips = <span id="example-input-4-1">[[3,2,7],[3,7,9],[8,3,9]]</span>, capacity = <span id="example-input-4-2">11</span>
35+
<strong>Output: </strong><span id="example-output-4">true</span>
36+
</pre>
37+
</div>
38+
</div>
39+
</div>
40+
41+
<div>
42+
<div>
43+
<div>
44+
<div>&nbsp;</div>
45+
</div>
46+
</div>
47+
</div>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ol>
53+
<li><code>trips.length &lt;= 1000</code></li>
54+
<li><code>trips[i].length == 3</code></li>
55+
<li><code>1 &lt;= trips[i][0] &lt;= 100</code></li>
56+
<li><code>0 &lt;= trips[i][1] &lt; trips[i][2] &lt;= 1000</code></li>
57+
<li><code>1 &lt;=&nbsp;capacity &lt;= 100000</code></li>
58+
</ol>
59+
60+
61+
**Related Topics**:
62+
[Greedy](https://leetcode.com/tag/greedy/)
63+
64+
**Similar Questions**:
65+
* [Meeting Rooms II (Medium)](https://leetcode.com/problems/meeting-rooms-ii/)
66+
67+
## Solution 1.
68+
69+
```cpp
70+
// OJ: https://leetcode.com/problems/car-pooling/
71+
// Author: github.com/lzl124631x
72+
// Time: O(T + P) where T is the size of `trips` and P is the count of distinct stops
73+
// Space: O(P)
74+
class Solution {
75+
public:
76+
bool carPooling(vector<vector<int>>& trips, int capacity) {
77+
map<int, int> m;
78+
for (auto &t : trips) {
79+
m[t[1]] += t[0];
80+
m[t[2]] -= t[0];
81+
}
82+
int cnt = 0;
83+
for (auto &[p, n] : m) {
84+
cnt += n;
85+
if (cnt > capacity) return false;
86+
}
87+
return true;
88+
}
89+
};
90+
```

0 commit comments

Comments
 (0)