博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 1102. Path With Maximum Minimum Value
阅读量:4622 次
发布时间:2019-06-09

本文共 2308 字,大约阅读时间需要 7 分钟。

原题链接在这里:

题目:

Given a matrix of integers A with R rows and C columns, find the maximum score of a path starting at [0,0] and ending at [R-1,C-1].

The score of a path is the minimum value in that path.  For example, the value of the path 8 →  4 →  5 →  9 is 4.

path moves some number of times from one visited cell to any neighbouring unvisited cell in one of the 4 cardinal directions (north, east, west, south).

 

Example 1:

Input: [[5,4,5],[1,2,6],[7,4,6]]Output: 4Explanation: The path with the maximum score is highlighted in yellow.

Example 2:

Input: [[2,2,1,2,2,2],[1,2,2,2,1,2]]Output: 2

Example 3:

Input: [[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0],[4,6,5,4,3]]Output: 3

Note:

  1. 1 <= R, C <= 100
  2. 0 <= A[i][j] <= 10^9

题解:

From A[0][0], put element with index into maxHeap, sorted by element. Mark it as visited.

When polling out the currrent, check its surroundings. If not visited before, put it into maxHeap.

Until we hit the A[m-1][n-1].

Time Complexity: O(m*n*logmn). m = A.length. n = A[0].length. maxHeap add and poll takes O(logmn).

Space: O(m*n).

AC Java:

1 class Solution { 2     int [][] dirs = {
{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; 3 4 public int maximumMinimumPath(int[][] A) { 5 int m = A.length; 6 int n = A[0].length; 7 8 PriorityQueue
maxHeap = 9 new PriorityQueue
((a, b) -> b[2] - a[2]);10 maxHeap.add(new int[]{0, 0, A[0][0]});11 boolean [][] visited = new boolean[m][n];12 visited[0][0] = true;13 14 int res = A[0][0];15 while(!maxHeap.isEmpty()){16 int [] cur = maxHeap.poll();17 res = Math.min(res, cur[2]);18 if(cur[0]==m-1 && cur[1]==n-1){19 return res;20 }21 22 for(int [] dir : dirs){23 int x = cur[0] + dir[0];24 int y = cur[1] + dir[1];25 if(x<0 || x>=m ||y<0 || y>=n || visited[x][y]){26 continue;27 }28 29 visited[x][y] = true;30 maxHeap.add(new int[]{x, y, A[x][y]});31 }32 }33 34 return res;35 }36 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11297106.html

你可能感兴趣的文章
XML之外部加载并创建节点
查看>>
vue+koa+mysql简易demo
查看>>
castle windsor学习-----Inline dependencies 依赖
查看>>
Java中获取键盘输入值的三种方法
查看>>
职场如何汇报工作-不会汇报工作如何混职场
查看>>
《大道至简》第二章 读后感
查看>>
Centos7 搭建Svn+Apache服务器
查看>>
PHP之时间函数
查看>>
Vue之阻止默认行为
查看>>
Django一些操作命令
查看>>
Python open()完整参数
查看>>
django里面DTL使用for循环时,获取当前循环次数使用{{forloop.counter}}
查看>>
Linux基础-host文件解析
查看>>
PHP "延迟静态绑定" 功能,static
查看>>
(二)部署solr7.1.0到tomcat
查看>>
HTML5+CSS3 效果网站集合
查看>>
JavaWeb开发小结
查看>>
使用BootStrap网格布局进行一次演示
查看>>
jenkins(3): jenkins执行shell命令
查看>>
Cake slicing UVA - 1629
查看>>