The trick here is to name the state correctly, then let the implementation follow. For 1094. Car Pooling, the solution in this repository is mainly a greedy solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are carPooling, stops.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(#trips), space: O(#stops)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 36 ms, faster than 8.45% of C++ online submissions for Car Pooling.
02//Memory Usage: 8.8 MB, less than 100.00% of C++ online submissions for Car Pooling.
03class Solution {
04public:
05 bool carPooling(vector<vector<int>>& trips, int capacity) {
06 //(coordinate, whether is start, people count)
07 vector<vector<int>> points;
08
09 for(vector<int>& trip : trips){
10 int c = trip[0], s = trip[1], e = trip[2];
11 points.push_back({s, 1, c});
12 points.push_back({e, 0, c});
13 }
14
15 sort(points.begin(), points.end());
16
17 for(vector<int>& point : points){
18 int p = point[0];
19 bool isStart = (bool)(point[1]);
20 int c = point[2];
21
22 if(isStart){
23 //start point
24 if(capacity < c){
25 return false;
26 }
27 capacity -= c;
28 }else{
29 //end point
30 capacity += c;
31 }
32 }
33
34 return true;
35 }
36};
37
38//https://leetcode.com/problems/car-pooling/discuss/317611/C%2B%2BJava-O(n)-Thousand-and-One-Stops
39//Runtime: 12 ms, faster than 62.92% of C++ online submissions for Car Pooling.
40//Memory Usage: 8.2 MB, less than 100.00% of C++ online submissions for Car Pooling.
41//time: O(#trips), space: O(#stops)
42class Solution {
43public:
44 bool carPooling(vector<vector<int>>& trips, int capacity) {
45 vector<int> stops(1001, 0);
46 for(auto trip : trips){
47 //how many people get in the car at this stop
48 stops[trip[1]] += trip[0];
49 stops[trip[2]] -= trip[0];
50 }
51
52 for(int i = 0; i < stops.size(); i++){
53 capacity -= stops[i];
54 if(capacity < 0) return false;
55 }
56
57 return true;
58 }
59};
Cost