-
Notifications
You must be signed in to change notification settings - Fork 0
/
138. Avoid Flood in City.cpp
38 lines (32 loc) · 1.15 KB
/
138. Avoid Flood in City.cpp
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
class Solution {
public:
vector<int> avoidFlood(vector<int>& rains) {
unordered_map<int,int> lake_filled;
int dry_days=0,n=rains.size();
vector<int> res(n,100057);
set<int> q;
for(int i=0; i<rains.size(); i++) {
if(rains[i]==0) {
dry_days++;
q.insert(i);
}else {
res[i]=-1;
if(lake_filled.find(rains[i])==lake_filled.end()) {
lake_filled[rains[i]]=i;
}
else{
if(dry_days<=0)
return {};
auto index=q.upper_bound(lake_filled[rains[i]]);
if(index==q.end())
return {};
int val=*index;
q.erase(index);
res[val]=rains[i];
lake_filled[rains[i]]=i;
}
}
}
return res;
}
};