-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1094-Car Pooling.java
More file actions
27 lines (27 loc) · 925 Bytes
/
1094-Car Pooling.java
File metadata and controls
27 lines (27 loc) · 925 Bytes
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
class Solution {
public boolean carPooling(int[][] trips, int capacity) {
int count = 0;
HashMap<Integer, Integer> destinations = new HashMap<>();
HashMap<Integer, Integer> arrivals = new HashMap<>();
int last = 0;
for(int[] trip : trips){
destinations.putIfAbsent(trip[2], 0);
destinations.put(trip[2], destinations.get(trip[2])+trip[0]);
last = Math.max(last,trip[2]);
arrivals.putIfAbsent(trip[1], 0);
arrivals.put(trip[1], arrivals.get(trip[1])+trip[0]);
}
for(int i =0;i<=last;i++){
if(arrivals.containsKey(i)){
count += arrivals.get(i);
}
if(destinations.containsKey(i)){
count -= destinations.get(i);
}
if(count>capacity){
return false;
}
}
return true;
}
}