Skip to content

Commit 5448c2b

Browse files
chore: add LeetCode daily solution
1 parent 1e0eee2 commit 5448c2b

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Avoid Flood in The City (Medium)
2+
3+
**Problem ID:** 1488
4+
**Date:** 2025-10-07
5+
**Link:** https://leetcode.com/problems/avoid-flood-in-the-city/
6+
7+
## Approach
8+
9+
To solve the "Avoid Flood in The City" problem, we can use a greedy approach combined with a hash set to keep track of the lakes that are currently full. The main idea is to process each day's rain and manage the drying of lakes effectively to avoid floods.
10+
11+
### Approach:
12+
13+
1. **Initialization**:
14+
- Create an array `ans` of the same length as `rains`, initialized with `-1` to indicate days when it rains.
15+
- Use a hash set `fullLakes` to track which lakes are currently full.
16+
- Use a list `dryDays` to record the indices of days when no rain occurs (i.e., `rains[i] == 0`).
17+
18+
2. **Processing Rain Days**:
19+
- Iterate through the `rains` array:
20+
- If `rains[i] > 0` (it rains on lake `rains[i]`):
21+
- Check if the lake is already in `fullLakes`. If it is, it means a flood will occur, and we should return an empty array.
22+
- Otherwise, add the lake to `fullLakes` and set `ans[i]` to `-1` (indicating rain on this day).
23+
- If `rains[i] == 0` (no rain):
24+
- If there are full lakes (i.e., `fullLakes` is not empty), we can choose one lake to dry. We can pick any lake from `fullLakes`, remove it from `fullLakes`, and record the dried lake in `ans[i]`.
25+
- If there are no full lakes, we can choose not to dry any lake, and `ans[i]` remains `-1`.
26+
27+
3. **Final Output**:
28+
- After processing all days, the `ans` array will contain the required output. If we encountered a situation where a flood was unavoidable, we would have returned an empty array earlier.
29+
30+
### Data Structures:
31+
- **Array**: To store the results for each day.
32+
- **Hash Set**: To track full lakes efficiently, allowing O(1) average time complexity for insertions and lookups.
33+
- **List**: To keep track of dry days, although it is not strictly necessary for the solution.
34+
35+
### Complexity:
36+
- **Time Complexity**: O(n), where n is the length of the `rains` array. We make a single pass through the array and perform constant-time operations for each element.
37+
- **Space Complexity**: O(m), where m is the number of unique lakes that have been filled. In the worst case, this can be up to O(n) if all lakes are unique.
38+
39+
This approach efficiently manages the constraints of the problem while ensuring that floods are avoided wherever possible.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[] avoidFlood(int[] rains) {
5+
int n = rains.length;
6+
int[] ans = new int[n];
7+
Arrays.fill(ans, -1);
8+
9+
Set<Integer> fullLakes = new HashSet<>();
10+
Queue<Integer> dryDays = new PriorityQueue<>();
11+
12+
for (int i = 0; i < n; i++) {
13+
if (rains[i] > 0) {
14+
int lake = rains[i];
15+
if (fullLakes.contains(lake)) {
16+
return new int[0]; // Flood occurs
17+
}
18+
fullLakes.add(lake);
19+
ans[i] = -1; // It rained, no drying
20+
dryDays.offer(i); // Store the day we can dry this lake
21+
} else {
22+
ans[i] = 1; // Default to drying lake 1
23+
}
24+
25+
// Dry lakes on days where it rained before
26+
while (!dryDays.isEmpty() && !fullLakes.isEmpty()) {
27+
int dryDay = dryDays.poll();
28+
if (ans[dryDay] == 1) {
29+
int lakeToDry = fullLakes.iterator().next();
30+
fullLakes.remove(lakeToDry);
31+
ans[dryDay] = lakeToDry; // Dry this lake
32+
}
33+
}
34+
}
35+
36+
return ans;
37+
}
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var avoidFlood = function(rains) {
2+
const n = rains.length;
3+
const ans = new Array(n).fill(-1);
4+
const fullLakes = new Set();
5+
const dryDays = [];
6+
7+
for (let i = 0; i < n; i++) {
8+
if (rains[i] > 0) {
9+
if (fullLakes.has(rains[i])) {
10+
return [];
11+
}
12+
fullLakes.add(rains[i]);
13+
} else {
14+
dryDays.push(i);
15+
}
16+
}
17+
18+
for (let i = 0; i < n; i++) {
19+
if (rains[i] > 0) {
20+
ans[i] = -1;
21+
const lake = rains[i];
22+
fullLakes.delete(lake);
23+
} else {
24+
if (fullLakes.size > 0) {
25+
const lakeToDry = fullLakes.values().next().value;
26+
ans[i] = lakeToDry;
27+
fullLakes.delete(lakeToDry);
28+
} else if (dryDays.length > 0) {
29+
ans[i] = 1; // Dry any lake (1 is arbitrary)
30+
}
31+
}
32+
}
33+
34+
return ans;
35+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def avoidFlood(self, rains: List[int]) -> List[int]:
3+
from collections import defaultdict
4+
5+
full_lakes = set()
6+
ans = [-1] * len(rains)
7+
dry_days = []
8+
9+
for i, lake in enumerate(rains):
10+
if lake > 0:
11+
if lake in full_lakes:
12+
return []
13+
full_lakes.add(lake)
14+
ans[i] = -1
15+
else:
16+
dry_days.append(i)
17+
ans[i] = 0
18+
19+
for lake in full_lakes:
20+
if not dry_days:
21+
return []
22+
dry_day = dry_days.pop(0)
23+
ans[dry_day] = lake
24+
25+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,4 @@ Each problem includes:
169169
- 2025-10-04 — [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) (Medium) → `Medium/2025-10-04-11-Container-With-Most-Water`
170170
- 2025-10-05 — [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) (Medium) → `Medium/2025-10-05-417-Pacific-Atlantic-Water-Flow`
171171
- 2025-10-06 — [Swim in Rising Water](https://leetcode.com/problems/swim-in-rising-water/) (Hard) → `Hard/2025-10-06-778-Swim-in-Rising-Water`
172+
- 2025-10-07 — [Avoid Flood in The City](https://leetcode.com/problems/avoid-flood-in-the-city/) (Medium) → `Medium/2025-10-07-1488-Avoid-Flood-in-The-City`

0 commit comments

Comments
 (0)