-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathCarPooling.java
285 lines (250 loc) · 9.99 KB
/
CarPooling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package LeetCodeJava.Array;
// https://leetcode.com/problems/car-pooling/description/
/**
*
1094. Car Pooling
Medium
Topics
Companies
Hint
There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively. The locations are given as the number of kilometers due east from the car's initial location.
Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.
Example 1:
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: false
Example 2:
Input: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: true
Constraints:
1 <= trips.length <= 1000
trips[i].length == 3
1 <= numPassengersi <= 100
0 <= fromi < toi <= 1000
1 <= capacity <= 105
*/
public class CarPooling {
// V0
// IDEA: PREFIX SUM + `prefix interval handling` (improve efficience)
public boolean carPooling(int[][] trips, int capacity) {
// edge
if (trips == null || trips.length == 0) {
return true;
}
if (trips.length == 1) {
return capacity > trips[0][0];
}
// init prefix sum
int[] prefixSum = new int[1001]; // the biggest array size given by problem
// `init pre prefix sum`
for (int[] t : trips) {
int amount = t[0];
int start = t[1];
int end = t[2];
/**
* NOTE !!!!
*
* via trick below, we can `efficiently` setup prefix sum
* per start, end index
*
* -> we ADD amount at start point (customer pickup up)
* -> we MINUS amount at `end point` (customer drop off)
*
* -> via above, we get the `adjusted` `init prefix sum`
* -> so all we need to do next is :
* -> loop over the `init prefix sum`
* -> and keep adding `previous to current val`
* -> e.g. prefixSum[i] = prefixSum[i-1] + prefixSum[i]
*
*/
prefixSum[start] += amount;
prefixSum[end] -= amount;
}
// update `prefix sum` array
for (int i = 1; i < prefixSum.length; i++) {
prefixSum[i] += prefixSum[i - 1];
}
// check if it's `possible` to get all passenger
for (int j : prefixSum) {
if (capacity < j) {
return false;
}
}
return true;
}
// V0-1
// IDEA: PREFIX SUM
/**
* NOTE !!!
*
* via
* - lengthOfTrip[trip[1]] += trip[0];
* - lengthOfTrip[trip[2]] -= trip[0];
*
* and
*
* - carLoad += lengthOfTrip[i];
*
*
* -> we can SIMULATE the `pickup` and `dropOff` scenario
*
*/
public boolean carPooling_0_1(int[][] trips, int capacity) {
// Because from and to is between 0 and 1000. Idea is to store counts in an array of size 1001.
int lengthOfTrip[] = new int[1001];
for (int trip[] : trips){
/**
*
* For each trip:
*
* - We increment the value at lengthOfTrip[trip[1]] by trip[0].
* This indicates that the given number of passengers (from trip[0])
* board the vehicle at the pick-up location (trip[1]).
*
*
* - We decrement the value at lengthOfTrip[trip[2]] by trip[0].
* This indicates that the same number of passengers (from trip[0])
* leave the vehicle at the drop-off location (trip[2]).
*
*/
lengthOfTrip[trip[1]] += trip[0]; // Increment when passenger is on board
lengthOfTrip[trip[2]] -= trip[0]; // decrement when they depart
}
// Count total passenger for each bus top
int carLoad = 0;
// we have the count array, we perform a line sweep from 0 to 1000 and track its total
for (int i = 0; i < 1001; i++){
carLoad += lengthOfTrip[i];
// Reject when the car is overloaded somewhere
if(carLoad > capacity) return false;
}
return true; // Accept only if all trip is safe
}
// V0-2
// IDEA: PREFIX SUM (gpt)
public boolean carPooling_0_2(int[][] trips, int capacity) {
// Edge case: if there are no trips, the vehicle is never needed
if (trips == null || trips.length == 0) {
return true;
}
// Find the maximum drop-off location to limit the size of the arrays
int maxLocation = 0;
for (int[] trip : trips) {
maxLocation = Math.max(maxLocation, trip[2]);
}
// Create a "timeline" to track passengers being picked up and dropped off
int[] passengersAtLocation = new int[maxLocation + 1];
// Populate the pick-up and drop-off changes
for (int[] trip : trips) {
int numPassengers = trip[0];
int pickUpLocation = trip[1];
int dropOffLocation = trip[2];
// Add passengers at the pick-up location
passengersAtLocation[pickUpLocation] += numPassengers;
// Subtract passengers at the drop-off location
passengersAtLocation[dropOffLocation] -= numPassengers;
}
// Now, check if at any point the number of passengers exceeds the capacity
int currentPassengers = 0;
for (int passengers : passengersAtLocation) {
currentPassengers += passengers;
if (currentPassengers > capacity) {
return false; // Capacity exceeded at some point
}
}
// If we never exceed capacity, return true
return true;
}
// V1
// https://youtu.be/08sn_w4LWEE?feature=shared
// https://github.com/neetcode-gh/leetcode/blob/main/java%2F1094-car-pooling.java
public boolean carPooling_1(int[][] trips, int capacity) {
int[] passChange = new int[1001];
for (int[] t : trips) {
passChange[t[1]] += t[0];
passChange[t[2]] -= t[0];
}
int curPass = 0;
for (int i = 0; i < 1001; i++) {
curPass += passChange[i];
if (curPass > capacity) {
return false;
}
}
return true;
}
// V2
// https://leetcode.com/problems/car-pooling/solutions/1669644/well-explained-2-waysjava-cpythonjavascr-djso/
public boolean carPooling_2(int[][] trips, int capacity) {
// Because from and to is between 0 and 1000. Idea is to store counts in an array of size 1001.
int lengthOfTrip[] = new int[1001];
for (int trip[] : trips){
/**
*
* For each trip:
*
* - We increment the value at lengthOfTrip[trip[1]] by trip[0].
* This indicates that the given number of passengers (from trip[0])
* board the vehicle at the pick-up location (trip[1]).
*
*
* - We decrement the value at lengthOfTrip[trip[2]] by trip[0].
* This indicates that the same number of passengers (from trip[0])
* leave the vehicle at the drop-off location (trip[2]).
*
*/
lengthOfTrip[trip[1]] += trip[0]; // Increment when passenger is on board
lengthOfTrip[trip[2]] -= trip[0]; // decrement when they depart
}
// Count total passenger for each bus top
int carLoad = 0;
// we have the count array, we perform a line sweep from 0 to 1000 and track its total
for (int i = 0; i < 1001; i++){
/**
* Sweeping Through the Stops:
*
*
* - After processing all trips, we now have the "net change in passengers"
* at each stop (location).
*
* - We loop through the lengthOfTrip[] array from index 0 to 1000 to
* simulate the journey and track the total number of passengers in
* the vehicle at each stop.
*
* - We maintain a variable carLoad which starts at 0 and keeps a running
* total of passengers in the vehicle at each stop.
*
* - At each location i, we add lengthOfTrip[i] to carLoad to account for
* the passengers boarding or leaving at that stop.
*
* - If at any point carLoad exceeds the vehicle's capacity,
* it means the car is overloaded, so we immediately return false.
*
* - If we successfully go through all stops without exceeding the capacity,
* we return true.
*
*/
carLoad += lengthOfTrip[i];
// Reject when the car is overloaded somewhere
if(carLoad > capacity) return false;
}
return true; // Accept only if all trip is safe
}
// V3
// https://leetcode.com/problems/car-pooling/solutions/1670309/cjavapython-donot-sort-on-95-faster-imag-da8q/
public boolean carPooling_3(int[][] trips, int capacity) {
int in_car = 0;
int[] increase = new int[1001];
for (int i = 0; i < trips.length; i++) { // find out the number of the **net increase** passengers for each stop
increase[trips[i][1]] += trips[i][0];
increase[trips[i][2]] -= trips[i][0];
}
for (int i = 0; i <= 1000; i++) { // from start to end, for each stop we calculate the number of passengers in
// the car.
in_car += increase[i];
if (in_car > capacity)
return false; // once the number of passengers in the car exceeds the capacity
}
return true;
}
}