zoukankan      html  css  js  c++  java
  • LeetCode 778. Swim in Rising Water

    原题链接在这里:https://leetcode.com/problems/swim-in-rising-water/

    题目:

    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].

    题解:

    Eventually, we want to hit the bottom right.

    Every time we try to find the minimum surroundings.

    Keep a minHeap based on elevation, when poll out from minHeap, update max elevation. 

    And if current position is bottom right, return res.

    Otherwise, for 4 directions, check if it is not visited, add {x, y, elevation} to minHeap.

    Time Complexity: O(N^2*logN). N = grid.length.

    Space: O(N^2). heap size.

    AC Java:

     1 class Solution {
     2     public int swimInWater(int[][] grid) {
     3         int N = grid.length;
     4         PriorityQueue<int []> minHeap = new PriorityQueue<>((a, b) -> a[2] - b[2]);
     5         boolean [][] visited = new boolean[N][N];
     6         
     7         minHeap.add(new int[]{0, 0, grid[0][0]});
     8         visited[0][0] = true;
     9         int res = 0;
    10         int[][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    11         
    12         while(!minHeap.isEmpty()){
    13             int [] cur = minHeap.poll();
    14             res = Math.max(res, cur[2]);
    15             if(cur[0] == N - 1 && cur[1] == N - 1){
    16                 return res;
    17             }
    18             
    19             for(int [] dir : dirs){
    20                 int x = cur[0] + dir[0];
    21                 int y = cur[1] + dir[1];
    22                 if(x < 0 || x >= N || y < 0 || y >= N || visited[x][y]){
    23                     continue;
    24                 }
    25                 
    26                 visited[x][y] = true;
    27                 minHeap.add(new int[]{x, y, grid[x][y]});
    28             }    
    29         }
    30         
    31         return -1;
    32     }
    33 }
  • 相关阅读:
    题目3:爬楼梯
    题目1:删除排序数组中的重复数字
    最近目标
    软件工程----个人总结
    软件工程第二次作业——结对编程
    软件工程第一次作业补充
    爬楼梯
    买卖股票的最佳时机
    删除排序数组中的重复数字
    思考题
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12248820.html
Copyright © 2011-2022 走看看