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 time0
, 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 time3
. When the depth of water is3
, 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:
2 <= N <= 50
.- grid[i][j] is a permutation of [0, …, N*N – 1].
Solution 1: Dijkstra’s Algorithm
Time complexity: O(n^2*logn)
Space complexity: O(n^2)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// Author: Huahua, running time: 8 ms class Solution { public: int swimInWater(vector<vector<int>>& grid) { const int n = grid.size(); priority_queue<pair<int, int>> q; // {-time, y * N + x} q.push({-grid[0][0], 0 * n + 0}); vector<int> seen(n * n); vector<int> dirs{-1, 0, 1, 0, -1}; seen[0 * n + 0] = 1; while (!q.empty()) { auto node = q.top(); q.pop(); int t = -node.first; int x = node.second % n; int y = node.second / n; if (x == n - 1 && y == n - 1) return t; for (int i = 0; i < 4; ++i) { int tx = x + dirs[i]; int ty = y + dirs[i + 1]; if (tx < 0 || ty < 0 || tx >= n || ty >= n) continue; if (seen[ty * n + tx]) continue; seen[ty * n + tx] = 1; q.push({-max(t, grid[ty][tx]), ty * n + tx}); } } return -1; } }; |
Solution 2: Binary Search + BFS
Time complexity: O(2logn * n^2)
Space complexity: O(n^2)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// Author: Huahua, running time: 8 ms class Solution { public: int swimInWater(vector<vector<int>>& grid) { const int n = grid.size(); auto hasPath = [&grid, n](int t) { if (grid[0][0] > t) return false; queue<int> q; vector<int> seen(n * n); vector<int> dirs{1, 0, -1, 0, 1}; q.push(0); while (!q.empty()) { const int x = q.front() % n; const int y = q.front() / n; q.pop(); if (x == n - 1 && y == n - 1) return true; for (int i = 0; i < 4; ++i) { const int tx = x + dirs[i]; const int ty = y + dirs[i + 1]; if (tx < 0 || ty < 0 || tx >= n || ty >= n || grid[ty][tx] > t) continue; const int key = ty * n + tx; if (seen[key]) continue; seen[key] = 1; q.push(key); } } return false; }; int l = 0; int r = n * n; while (l < r) { int m = l + (r - l) / 2; if (hasPath(m)) { r = m; } else { l = m + 1; } } return l; } }; |