• 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 }
  • 相关阅读:
    超棒的前端开发界面套件 InK
    现代浏览器的web音频javascript类库 Howler.js
    富有创意的菱形响应式页面设计
    创意味儿十足的web布局及交互设计
    一个超酷的横向多列响应式布局效果
    帮助你生成响应式布局的CSS模板 xyCSS
    免费素材大荟萃:免费图标和UI设计
    使用浏览器生成超棒的midi音乐 midi.js
    JavaScript 和 .NET 中的 JavaScript Object Notation (JSON) 简介
    推荐一批基于web的开源html文本编辑器(40+)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12248820.html
Copyright © 2020-2023  润新知