Skip to content

Latest commit

 

History

History
 
 

778. Swim in Rising Water

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j).

Now 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 distance in zero time. Of course, you must stay within the boundaries of the grid during your swim.

You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (N-1, N-1)?

Example 1:

Input: [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0).
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.

You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.

Example 2:

Input: [[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]]
Output: 16
Explanation:
 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

The final route is marked in bold.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.

Note:

  1. 2 <= N <= 50.
  2. grid[i][j] is a permutation of [0, ..., N*N - 1].

Companies:
Facebook

Related Topics:
Binary Search, Heap, Depth-first Search, Union Find

Solution 1. Binary Answer

// OJ: https://leetcode.com/problems/swim-in-rising-water/
// Author: github.com/lzl124631x
// Time: O(N^2 * logN)
// Space: O(N^2)
class Solution {
    int N, dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    vector<vector<bool>> seen;
    bool dfs(vector<vector<int>> &G, int x, int y, int height) {
        if (G[x][y] > height) return false;
        if (x == N - 1 && y == N - 1) return true;
        seen[x][y] = true;
        for (auto &[dx, dy] : dirs) {
            int a = x + dx, b = y + dy;
            if (a < 0 || a >= N || b < 0 || b >= N || seen[a][b]) continue;
            if (dfs(G, a, b, height)) return true;
        }
        return false;
    }
    bool valid(vector<vector<int>>& G, int height) {
        seen.assign(N, vector<bool>(N, false));
        return dfs(G, 0, 0, height);
    }
public:
    int swimInWater(vector<vector<int>>& G) {
        N = G.size();
        int L = 0, R = N * N - 1;
        while (L <= R) {
            int M = (L + R) / 2;
            if (valid(G, M)) R = M - 1;
            else L = M + 1;
        }
        return L;
    }
};

Solution 2. Heap

// OJ: https://leetcode.com/problems/swim-in-rising-water/
// Author: github.com/lzl124631x
// Time: O(N^2 * logN)
// Space: O(N^2)
class Solution {
    typedef tuple<int, int, int> Point; // height, x, y
public:
    int swimInWater(vector<vector<int>>& G) {
        priority_queue<Point, vector<Point>, greater<>> pq;
        pq.emplace(G[0][0], 0, 0);
        int ans = 0, N = G.size(), dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
        while (true) {
            auto [height, x, y] = pq.top();
            pq.pop();
            ans = max(ans, height);
            if (x == N - 1 && y == N- 1) return ans;
            for (auto &[dx, dy] : dirs) {
                int a = x + dx, b = y + dy;
                if (a < 0 || a >= N || b < 0 || b >= N || G[a][b] == -1) continue;
                pq.emplace(G[a][b], a, b);
                G[a][b] = -1;
            }
        }
    }
};