Skip to content

Commit 1e0eee2

Browse files
chore: add LeetCode daily solution
1 parent 8a12698 commit 1e0eee2

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Swim in Rising Water (Hard)
2+
3+
**Problem ID:** 778
4+
**Date:** 2025-10-06
5+
**Link:** https://leetcode.com/problems/swim-in-rising-water/
6+
7+
## Approach
8+
9+
To solve the "Swim in Rising Water" problem, we can utilize a combination of a priority queue (min-heap) and a breadth-first search (BFS) strategy to efficiently determine the minimum time required to reach the bottom-right corner of the grid from the top-left corner.
10+
11+
### Main Idea:
12+
The key insight is that the problem can be viewed as a pathfinding problem where the cost of moving through a cell is determined by the water level (time `t`). The goal is to find the minimum `t` such that there exists a path from the starting cell `(0, 0)` to the target cell `(n-1, n-1)` where all cells in the path have elevations less than or equal to `t`.
13+
14+
### Approach:
15+
1. **Min-Heap for Priority Queue**: Use a min-heap to explore cells in the order of their elevation values. This allows us to always consider the lowest elevation cell that is reachable at the current water level.
16+
17+
2. **BFS with Priority Queue**: Start from the top-left cell `(0, 0)` and push it into the min-heap. While the heap is not empty:
18+
- Extract the cell with the minimum elevation (this represents the current water level we can swim at).
19+
- Check if this cell is the bottom-right cell. If it is, the elevation at this cell is the minimum time required to reach it.
20+
- Otherwise, explore the 4-directionally adjacent cells (up, down, left, right). For each adjacent cell, if it is within bounds and has not been visited, push it into the heap with its elevation.
21+
22+
3. **Visited Set**: Maintain a set to keep track of visited cells to prevent reprocessing and infinite loops.
23+
24+
### Data Structures:
25+
- **Min-Heap (Priority Queue)**: To efficiently get the cell with the minimum elevation.
26+
- **Set/Array for Visited Cells**: To track which cells have already been processed.
27+
28+
### Complexity:
29+
- **Time Complexity**: O(n^2 log(n)), where n is the dimension of the grid. Each cell is pushed and popped from the heap once, and heap operations take logarithmic time.
30+
- **Space Complexity**: O(n^2) for the visited set and the heap, which can store all cells in the worst case.
31+
32+
This approach ensures that we find the minimum time required to swim to the target cell while efficiently managing the elevation constraints imposed by the grid.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.PriorityQueue;
2+
3+
class Solution {
4+
public int swimInWater(int[][] grid) {
5+
int n = grid.length;
6+
int[][] directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
7+
boolean[][] visited = new boolean[n][n];
8+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
9+
pq.offer(new int[]{grid[0][0], 0, 0});
10+
visited[0][0] = true;
11+
12+
while (!pq.isEmpty()) {
13+
int[] current = pq.poll();
14+
int time = current[0];
15+
int x = current[1];
16+
int y = current[2];
17+
18+
if (x == n - 1 && y == n - 1) {
19+
return time;
20+
}
21+
22+
for (int[] dir : directions) {
23+
int newX = x + dir[0];
24+
int newY = y + dir[1];
25+
26+
if (newX >= 0 && newX < n && newY >= 0 && newY < n && !visited[newX][newY]) {
27+
visited[newX][newY] = true;
28+
pq.offer(new int[]{Math.max(time, grid[newX][newY]), newX, newY});
29+
}
30+
}
31+
}
32+
return -1; // This line should never be reached
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var swimInWater = function(grid) {
2+
const n = grid.length;
3+
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
4+
let left = 0, right = Math.max(...grid.flat());
5+
6+
const canSwim = (t) => {
7+
const visited = Array.from({ length: n }, () => Array(n).fill(false));
8+
const queue = [[0, 0]];
9+
visited[0][0] = true;
10+
11+
while (queue.length) {
12+
const [x, y] = queue.shift();
13+
if (x === n - 1 && y === n - 1) return true;
14+
15+
for (const [dx, dy] of directions) {
16+
const nx = x + dx, ny = y + dy;
17+
if (nx >= 0 && nx < n && ny >= 0 && ny < n && !visited[nx][ny] && grid[nx][ny] <= t) {
18+
visited[nx][ny] = true;
19+
queue.push([nx, ny]);
20+
}
21+
}
22+
}
23+
return false;
24+
};
25+
26+
while (left < right) {
27+
const mid = Math.floor((left + right) / 2);
28+
if (canSwim(mid)) {
29+
right = mid;
30+
} else {
31+
left = mid + 1;
32+
}
33+
}
34+
35+
return left;
36+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import heapq
2+
3+
class Solution:
4+
def swimInWater(self, grid: List[List[int]]) -> int:
5+
n = len(grid)
6+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
7+
min_heap = [(grid[0][0], 0, 0)]
8+
visited = set((0, 0))
9+
10+
while min_heap:
11+
t, x, y = heapq.heappop(min_heap)
12+
if (x, y) == (n - 1, n - 1):
13+
return t
14+
15+
for dx, dy in directions:
16+
nx, ny = x + dx, y + dy
17+
if 0 <= nx < n and 0 <= ny < n and (nx, ny) not in visited:
18+
visited.add((nx, ny))
19+
heapq.heappush(min_heap, (max(t, grid[nx][ny]), nx, ny))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,4 @@ Each problem includes:
168168
- 2025-10-03 — [Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) (Hard) → `Hard/2025-10-03-407-Trapping-Rain-Water-II`
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`
171+
- 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`

0 commit comments

Comments
 (0)