Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 1488. Avoid Flood in The City
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
vector<int> avoidFlood(vector<int>& rains) {
set<int> zer; // stores indices of days we can dry (rains[i] == 0)
int n = rains.size();
vector<int> ans(n, -1); // result array
unordered_map<int, int> M; // lake -> last day it was filled

for (int i = 0; i < n; i++) {
if (rains[i] == 0) {
// We can dry some lake later
zer.insert(i);
ans[i] = 1; // default, can be updated later
} else {
int lake = rains[i];
// If lake already has water, we need to dry it before raining again
if (M.find(lake) != M.end()) {
int lastRainDay = M[lake];
// Find first zero-day after last rain day
auto up = zer.upper_bound(lastRainDay);
if (up == zer.end()) {
// No zero-day available to dry => flood
return {};
}
// Use that day to dry the lake
ans[*up] = lake;
zer.erase(up);
}
// Update the last rain day for this lake
M[lake] = i;
}
}
return ans;
}
};
Loading