原题链接:
https://leetcode.cn/problems/avoid-flood-in-the-city/description
解法1: 贪心 + 二分
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
class Solution { public: vector<int> avoidFlood(vector<int>& rains) { unordered_map<int,int>water; set<int>zero; vector<int>ans(rains.size(),1);
for(int i = 0 ; i < rains.size() ; i++){ int r = rains[i]; if(r == 0){ zero.insert(i); continue; }
if(water.count(r) != 0){ int pre = water[r]; auto it = zero.lower_bound(pre); if(it == zero.end()){ return {}; }
ans[*it] = r; zero.erase(it); }
water[r] = i; ans[i] = -1; }
return ans; } };
|