Skip to content
Merged
Show file tree
Hide file tree
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
537 changes: 43 additions & 494 deletions solution/1400-1499/1488.Avoid Flood in The City/README.md

Large diffs are not rendered by default.

537 changes: 43 additions & 494 deletions solution/1400-1499/1488.Avoid Flood in The City/README_EN.md

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions solution/1400-1499/1488.Avoid Flood in The City/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ func avoidFlood(rains []int) []int {
for i := range ans {
ans[i] = -1
}
sunny := []int{}

sunny := redblacktree.New[int, struct{}]()
rainy := map[int]int{}

for i, v := range rains {
if v > 0 {
if j, ok := rainy[v]; ok {
idx := sort.SearchInts(sunny, j+1)
if idx == len(sunny) {
if last, ok := rainy[v]; ok {
node, found := sunny.Ceiling(last + 1)
if !found {
return []int{}
}
ans[sunny[idx]] = v
sunny = append(sunny[:idx], sunny[idx+1:]...)
t := node.Key
ans[t] = v
sunny.Remove(t)
}
rainy[v] = i
} else {
sunny = append(sunny, i)
sunny.Put(i, struct{}{})
ans[i] = 1
}
}
return ans
}
}
28 changes: 28 additions & 0 deletions solution/1400-1499/1488.Avoid Flood in The City/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::collections::{BTreeSet, HashMap};

impl Solution {
pub fn avoid_flood(rains: Vec<i32>) -> Vec<i32> {
let n = rains.len();
let mut ans = vec![-1; n];
let mut sunny = BTreeSet::new();
let mut rainy = HashMap::new();

for (i, &v) in rains.iter().enumerate() {
if v > 0 {
if let Some(&last) = rainy.get(&v) {
if let Some(&t) = sunny.range((last + 1) as usize..).next() {
ans[t] = v;
sunny.remove(&t);
} else {
return vec![];
}
}
rainy.insert(v, i as i32);
} else {
sunny.insert(i);
ans[i] = 1;
}
}
ans
}
}
Loading