You are given a car route represented by locationCount locations numbered from 0 to locationCount - 1.
The car only moves east, from smaller location numbers to larger location numbers.
You are also given a list trips. Each trip is represented as a string in the format "passengers,from,to".
A trip "passengers,from,to" means passengers people get into the car at location from and get out of the car at location to.
Since passengers get out at location to, they are present in the car for every location from from to to - 1. This means each trip affects the half-open range [from, to).
Return a list where the value at index i represents the number of passengers inside the car while traveling through location segment i.
Method Signature
public List<Integer> getPassengerCountAfterTrips(int locationCount, List<String> trips)
Input Format
locationCount: The number of locations in the route.
trips: A list of trip strings. Each trip string has the format "passengers,from,to".
Output Format
Return a List<Integer> of size locationCount.
The value at index i is the total number of passengers inside the car for location segment i.
Rules
- The car only moves east, so
from will always be less than to.
- A trip affects locations from
from to to - 1.
- A passenger dropped at location
to is not counted at location to.
- Multiple trips may overlap, and passenger counts should be added together.
- The returned list should include passenger counts for all locations from
0 to locationCount - 1.
Constraints
1 <= locationCount <= 100,000
0 <= trips.size() <= 100,000
- Each trip string is in the format
"passengers,from,to".
1 <= passengers <= 100
0 <= from < to <= locationCount
Examples
Example 1
getPassengerCountAfterTrips(locationCount = 8, trips = ["2,1,5","3,3,7"])
Output: [0,2,2,5,5,3,3,0]
Explanation:
Trip "2,1,5" adds 2 passengers to locations 1, 2, 3, and 4.
Trip "3,3,7" adds 3 passengers to locations 3, 4, 5, and 6.
Final passenger counts are [0,2,2,5,5,3,3,0].
Example 2
getPassengerCountAfterTrips(locationCount = 5, trips = ["4,0,2","1,2,5","3,1,4"])
Output: [4,7,4,4,1]
Explanation:
Trip "4,0,2" affects locations 0 and 1.
Trip "1,2,5" affects locations 2, 3, and 4.
Trip "3,1,4" affects locations 1, 2, and 3.
Final passenger counts are [4,7,4,4,1].
Example 3
getPassengerCountAfterTrips(locationCount = 4, trips = [])
Output: [0,0,0,0]
Explanation:
There are no trips, so every location has 0 passengers.