|
| 1 | +# Car Pooling |
| 2 | + |
| 3 | +You are given a car with a fixed number of seats, denoted by an integer capacity. The car only travels in one direction |
| 4 | +— eastward — and does not make any U-turns. |
| 5 | + |
| 6 | +You are also provided with an array, trips, where each element trips[i]= [numPassengersi, fromi, toi] represents a group of |
| 7 | +numPassengersi that must be picked up at location fromi and dropped off at location toi . All locations are measured in |
| 8 | +kilometers east of the starting point. |
| 9 | + |
| 10 | +Your task is to determine whether it is possible to complete all the trips without exceeding the car’s capacity at any |
| 11 | +point in time. |
| 12 | + |
| 13 | +Return TRUE if all trips can be completed successfully, or FALSE otherwise. |
| 14 | + |
| 15 | +## Constraints |
| 16 | + |
| 17 | +- 1 <= `trips.length` <= 1000 |
| 18 | +- `trips[i].length` == 3 |
| 19 | +- 1 <= `numPassengers` <= 1000 |
| 20 | +- 0 <= fromi < toi <= 1000 |
| 21 | +- 1 <= `capacity` <= 10^5 |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +The core intuition behind this solution is to simulate the journey of the car using a timeline, tracking when passengers |
| 33 | +get in and get out. This approach aligns with the merge intervals pattern, where we focus on events happening at specific |
| 34 | +points in time, rather than handling each trip separately in isolation. The idea is to accumulate and monitor the number |
| 35 | +of passengers in the car at any moment using a difference array (also known as a prefix sum technique). |
| 36 | + |
| 37 | +In the context of this problem, each trip defines a passenger change over a specific interval — passengers are picked up |
| 38 | +at from and dropped off at to. Instead of iterating over the entire interval (which is inefficient), we can track just |
| 39 | +the start and end of the interval using a fixed-size timeline array. |
| 40 | + |
| 41 | +This strategy is built on two key observations: |
| 42 | + |
| 43 | +1. Each trip contributes a net passenger change at exactly two locations: |
| 44 | + - Increase at the pickup location (from) |
| 45 | + - Decrease at the drop-off location (to) |
| 46 | + |
| 47 | +2. The car’s capacity must never be exceeded at any point on the journey, so we monitor the cumulative passenger count |
| 48 | + as we move forward in time. |
| 49 | + |
| 50 | +This strategy leverages the difference array pattern, which is a version of merge intervals. Rather than merging |
| 51 | +overlapping intervals explicitly, it simulates all trips together via a timeline of passenger changes. |
| 52 | + |
| 53 | +Let’s break down the steps: |
| 54 | + |
| 55 | +1. A list timestamp of size 1001 (based on constraints) is created to track changes in passenger counts at each kilometer |
| 56 | + point. |
| 57 | +2. For each trip `[numPassengersi ,fromi, toi]` in trips: |
| 58 | + - Add numPassengers at `timestamp[fromi]` |
| 59 | + - Subtract numPassengers at `timestamp[toi]` |
| 60 | + |
| 61 | + This marks the start and end of each passenger interval without iterating over the full range. |
| 62 | + |
| 63 | +3. To simulate the trip and track capacity, initialize a variable used_capacity = 0. |
| 64 | +4. Iterate through the timestamp list, adding the passenger changes at each point. If at any point used_capacity > capacity, |
| 65 | + return FALSE immediately— the car is overloaded. |
| 66 | +5. If the full timeline is processed without exceeding capacity, return TRUE, indicating that all trips can be accommodated. |
| 67 | + |
| 68 | +Let’s look at the following illustration to get a better understanding of the solution: |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +### Time Complexity |
| 84 | + |
| 85 | +The overall time complexity of the solution is O(n) because: |
| 86 | + |
| 87 | +- The trips list is scanned once to record passenger changes at pickup and drop-off points. |
| 88 | +- Each trip results in two constant-time operations: one increment and one decrement in the timeline array. |
| 89 | +- The timestamp array (of fixed length 1001) is traversed once to simulate the journey and check capacity constraints. |
| 90 | + |
| 91 | +As the length of timestamp is constant and independent of input size, it contributes O(1) time. So, the total operations |
| 92 | +scale linearly with the number of trips. |
| 93 | + |
| 94 | +### Space Complexity |
| 95 | + |
| 96 | +The space complexity of the solution is O(1) because a fixed-size array timestamp of length 1001 is used regardless of |
| 97 | +the input size. |
0 commit comments