778. Swim in Rising Water Hard
#Array#Binary Search#Depth-First Search#Breadth-First Search#Union Find#Heap (Priority Queue)#Matrix
1/**
2 * [778] Swim in Rising Water
3 *
4 * You are given an n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (i, j).
5 * The rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t. You can swim infinite distances in zero time. Of course, you must stay within the boundaries of the grid during your swim.
6 * Return the least time until you can reach the bottom right square (n - 1, n - 1) if you start at the top left square (0, 0).
7 *
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/swim1-grid.jpg" style="width: 164px; height: 165px;" />
10 * Input: grid = [[0,2],[1,3]]
11 * Output: 3
12 * Explanation:
13 * At time 0, you are in grid location (0, 0).
14 * You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.
15 * You cannot reach point (1, 1) until time 3.
16 * When the depth of water is 3, we can swim anywhere inside the grid.
17 *
18 * Example 2:
19 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/swim2-grid-1.jpg" style="width: 404px; height: 405px;" />
20 * Input: grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
21 * Output: 16
22 * Explanation: The final route is shown.
23 * We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
24 *
25 *
26 * Constraints:
27 *
28 * n == grid.length
29 * n == grid[i].length
30 * 1 <= n <= 50
31 * 0 <= grid[i][j] < n^2
32 * Each value grid[i][j] is unique.
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/swim-in-rising-water/
38// discuss: https://leetcode.com/problems/swim-in-rising-water/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn swim_in_water(grid: Vec<Vec<i32>>) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_778() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.