-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1094.car-pooling.java
More file actions
25 lines (23 loc) · 848 Bytes
/
Copy path1094.car-pooling.java
File metadata and controls
25 lines (23 loc) · 848 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
class Solution {
public boolean carPooling(int[][] trips, int capacity) {
HashMap<String, Integer> hsm = new HashMap<String, Integer>();
int maxv = -1;
for(int[] trip: trips){
maxv = Math.max(maxv, Math.max(trip[1], trip[2]));
hsm.put(trip[1]+"S", hsm.getOrDefault(trip[1]+"S", 0)+trip[0]);
hsm.put(trip[2]+"E", hsm.getOrDefault(trip[2]+"E", 0)+trip[0]);
}
System.out.println(hsm);
for(int i=0;i<=maxv;i++){
String start = i+"S";
String end = i+"E";
if(hsm.containsKey(start))
capacity -= hsm.get(start);
if(hsm.containsKey(end))
capacity += hsm.get(end);
if(capacity < 0)
return false;
}
return true;
}
}