-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-43.java
More file actions
29 lines (29 loc) · 934 Bytes
/
leet-code-q-43.java
File metadata and controls
29 lines (29 loc) · 934 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
28
29
class Solution {
public int[] avoidFlood(int[] rains) {
int n= rains.length;
int[] ans= new int[n];
Map<Integer, Integer> last_rain= new HashMap<>();
int def= 5;
TreeSet<Integer> dry_days= new TreeSet<>();
for(int i=0; i<n; i++){
int lake= rains[i];
if(lake==0){
dry_days.add(i);
ans[i]= def;
}else{
ans[i]= -1;
if(last_rain.containsKey(lake)){
Integer day= dry_days.higher(last_rain.get(lake));
//next dry day of last_rain.get(lake)
//[2 , 3, 4, 5, 9, 10]
//6
if(day==null) return new int[0];
ans[day]= lake;
dry_days.remove(day);
}
last_rain.put(lake, i);
}
}
return ans;
}
}