Skip to content

Commit e190a71

Browse files
committed
feat(1488): Avoid Flood in The City
1 parent e7c26b9 commit e190a71

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

1488. Avoid Flood in The City.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake.
2+
3+
// Given an integer array rains where:
4+
5+
// rains[i] > 0 means there will be rains over the rains[i] lake.
6+
// rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it.
7+
// Return an array ans where:
8+
9+
// ans.length == rains.length
10+
// ans[i] == -1 if rains[i] > 0.
11+
// ans[i] is the lake you choose to dry in the ith day if rains[i] == 0.
12+
// If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.
13+
14+
// Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)
15+
16+
// Example 1:
17+
18+
// Input: rains = [1,2,3,4]
19+
// Output: [-1,-1,-1,-1]
20+
// Explanation: After the first day full lakes are [1]
21+
// After the second day full lakes are [1,2]
22+
// After the third day full lakes are [1,2,3]
23+
// After the fourth day full lakes are [1,2,3,4]
24+
// There's no day to dry any lake and there is no flood in any lake.
25+
26+
// Example 2:
27+
28+
// Input: rains = [1,2,0,0,2,1]
29+
// Output: [-1,-1,2,1,-1,-1]
30+
// Explanation: After the first day full lakes are [1]
31+
// After the second day full lakes are [1,2]
32+
// After the third day, we dry lake 2. Full lakes are [1]
33+
// After the fourth day, we dry lake 1. There is no full lakes.
34+
// After the fifth day, full lakes are [2].
35+
// After the sixth day, full lakes are [1,2].
36+
// It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.
37+
38+
// Example 3:
39+
40+
// Input: rains = [1,2,0,1,2]
41+
// Output: []
42+
// Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day.
43+
// After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.
44+
45+
// Example 4:
46+
47+
// Input: rains = [69,0,0,0,69]
48+
// Output: [-1,69,1,1,-1]
49+
// Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9
50+
51+
// Example 5:
52+
53+
// Input: rains = [10,20,20]
54+
// Output: []
55+
// Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.
56+
57+
58+
// Constraints:
59+
60+
// 1 <= rains.length <= 10^5
61+
// 0 <= rains[i] <= 10^9
62+
63+
64+
/**
65+
* @param {number[]} rains
66+
* @return {number[]}
67+
*/
68+
const avoidFlood = (rains) => {
69+
let res = []
70+
// fulllake,{ lake: day }
71+
let fulllakes = new Map()
72+
// drydays: { day }, available day indexs to dry lakes
73+
let drydays = new Set()
74+
for (let i = 0; i < rains.length; i++) {
75+
if (rains[i] === 0) {
76+
drydays.add(i)
77+
res.push(1)
78+
} else {
79+
let lake = rains[i]
80+
let fulllake = fulllakes.has(lake)
81+
if (!fulllake) {
82+
fulllakes.set(lake, i)
83+
res.push(-1)
84+
} else {
85+
// ! Time Limit Exceeded
86+
// let filterDays = new Set([...drydays.keys()].filter(dryday => dryday > fulllakes.get(lake)))
87+
// if (filterDays.size === 0) {
88+
// return []
89+
// }
90+
// let dryday = filterDays.keys().next().value
91+
// res[dryday] = lake
92+
// drydays.delete(dryday)
93+
// fulllakes.set(lake, i)
94+
// res.push(-1)
95+
let dryLake = false
96+
if (drydays.size === 0) {
97+
return []
98+
}
99+
for (const dryday of drydays) {
100+
if (dryday > fulllakes.get(lake)) {
101+
res[dryday] = lake
102+
drydays.delete(dryday)
103+
fulllakes.set(lake, i)
104+
dryLake = true
105+
res.push(-1)
106+
break
107+
}
108+
}
109+
if (!dryLake) {
110+
return []
111+
}
112+
}
113+
}
114+
}
115+
return res
116+
}
117+
// Runtime: 556 ms, faster than 100.00% of JavaScript online submissions for Avoid Flood in The City.
118+
// Memory Usage: 65.9 MB, less than 100.00% of JavaScript online submissions for Avoid Flood in The City.
119+
120+
// Test case:
121+
122+
// let rains1 = [1,2,3,4]
123+
// let rains2 = [1,2,0,0,2,1]
124+
// let rains3 = [1,2,0,2,0,1]
125+
// let rains4 = [1,2,0,1,2]
126+
// let rains5 = [69,0,0,0,69]
127+
// let rains6 = [10,20,20]
128+
// let rains7 = [1,2,0,2,3,0,1]
129+
// let rains8 = [1,0,2,0,2,1]
130+
// console.log(avoidFlood(rains1)) // (4) [-1, -1, -1, -1]
131+
// console.log(avoidFlood(rains2)) // (6) [-1, -1, 1, 2, -1, -1]
132+
// console.log(avoidFlood(rains3)) // (6) [-1, -1, 2, -1, 1, -1]
133+
// console.log(avoidFlood(rains4)) // []
134+
// console.log(avoidFlood(rains5)) // (5) [-1, 69, 1, 1, -1]
135+
// console.log(avoidFlood(rains6)) // []
136+
// console.log(avoidFlood(rains7)) // [-1,-1,2,-1,-1,1,-1]
137+
// console.log(avoidFlood(rains8)) // [-1,1,-1,2,-1,-1]

0 commit comments

Comments
 (0)