【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题
【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)
题目描述:
You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col). You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (rows-1, columns-1) (i.e., 0-indexed). You can move up, down, left, or right, and you wish to find a route that requires the minimum effort.
A route’s effort is the maximum absolute difference in heights between two consecutive cells of the route.
Return the minimum effort required to travel from the top-left cell to the bottom-right cell.
Example 1:
Input: heights = [[1,2,2],[3,8,2],[5,3,5]]
Output: 2
Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
Example 2:
Input: heights = [[1,2,3],[3,8,4],[5,3,5]]
Output: 1
Explanation: The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
Example 3:
Input: heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
Output: 0
Explanation: This route does not require any effort.
Constraints:
- rows == heights.length
- columns == heights[i].length
- 1 <= rows, columns <= 100
- 1 <= heights[i][j] <= 10^6
题目大意
你准备参加一场远足活动。给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row, col) 的高度。一开始你在最左上角的格子 (0, 0) ,且你希望去最右下角的格子 (rows-1, columns-1) (注意下标从 0 开始编号)。你每次可以往 上,下,左,右 四个方向之一移动,你想要找到耗费 体力 最小的一条路径。
一条路径耗费的 体力值 是路径上相邻格子之间 高度差绝对值 的 最大值 决定的。
请你返回从左上角走到右下角的最小 体力消耗值 。
解题方法
- 采用求最短路径的Dijkstra算法(Dijkstra算法的原理:最短路径 | 深入浅出Dijkstra算法(一))
class Solution {public int minimumEffortPath(int[][] heights) {if (heights.length == 0 || heights[0].length == 0) return 0;int[][] ori = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};int m = heights.length;int n = heights[0].length;PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> (a[2] - b[2]));int[] distance = new int[m * n];boolean[] checked = new boolean[distance.length];Arrays.fill(distance, Integer.MAX_VALUE);queue.offer(new int[]{0, 0, 0});distance[0] = 0;while (queue.size() > 0) {int[] cur = queue.poll();int x = cur[0];int y = cur[1];int d = cur[2];int index = x * n + y;if (checked[index]) continue;checked[index] = true;for (int i = 0; i < ori.length; i++) {int nextX = x + ori[i][0];int nextY = y + ori[i][1];if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) continue;int nextIndex = nextX * n + nextY;int temp = Math.max(d, Math.abs(heights[x][y] - heights[nextX][nextY]));if (temp < distance[nextIndex]) {distance[nextIndex] = temp;queue.offer(new int[]{nextX, nextY, temp});}}}return distance[m * n - 1];}
}
执行耗时:86 ms,击败了68.69% 的Java用户
内存消耗:38.9 MB,击败了77.82% 的Java用户
【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题相关推荐
- 1631. 最小体力消耗路径
链接:1631. 最小体力消耗路径 题解:https://leetcode-cn.com/problems/path-with-minimum-effort/solution/duo-tu-xiang ...
- LeetCode 1631. 最小体力消耗路径(DFS + 二分查找)
文章目录 1. 题目 2. 解题 1. 题目 你准备参加一场远足活动.给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row ...
- leetcode 1631. 最小体力消耗路径(并查集)
你准备参加一场远足活动.给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row, col) 的高度.一开始你在最左上角的格子 ...
- Java每日一题——>剑指 Offer II 035. 最小时间差(三解,蛮力,排序,哈希)
文章目录 题目 题解1(蛮力法) 代码实现 复杂度分析 题解2(排序) 代码实现 复杂度分析 题解3(哈希表) 代码实现 复杂度分析 题目 这是LeetCode上的 [035,最小时间差],难度为 [ ...
- 【LeetCode笔记】238. 除自身以外数组的乘积(Java、思路题)
文章目录 题目描述 思路 & 代码 更新版 题目描述 首先暴力二层循环肯定可以,然后先累乘整个数组,再用除法肯定也行. 但是很遗憾,两种做法都不满足题目说明. 思路 & 代码 O(n) ...
- 【LeetCode】【HOT】155. 最小栈(辅助栈)
[LeetCode][HOT]155. 最小栈 文章目录 [LeetCode][HOT]155. 最小栈 package hot;import java.util.ArrayDeque; import ...
- LeetCode 64. Minimum Path Sum(最小和的路径)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Leetcode中Path的题目总结
Matrix中的path问题: 62.Unique Paths 63.Unique Paths II 这两题是典型的dp问题,要求一共有多少种方案.分析状态f(i, j)是走到(i, j)总共的路径. ...
- 《LeetCode力扣练习》第64题 最小路径和 Java
<LeetCode力扣练习>第64题 最小路径和 Java 一.资源 题目: 给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为 ...
最新文章
- 深度学习核心技术精讲100篇(六十三)-【CNN】一文详细讲解前因后果
- InstallShield 2011新功能试用(3)- Script Editor Intellisense
- Windows 平台下解决httpd.exe: syntax error on line 39
- 水晶报表基础入门——1.水晶报表技术
- [机器学习]超参数优化算法-SuccessiveHalving与Hyperband
- 最难啃的《深度学习》圣经花书,居然新出版了视频课!
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
- 【JavaScript】一个同步于本地时间的动态时间
- android系统打印功能实现,Android实现系统打印功能
- 2-7 hash(2)
- PHP 二维数组根据某个字段排序
- 与矩阵的秩有关的结论
- 常见几种编码格式及比较
- (伪)点到线段的距离 C++
- 01单片机——基础知识
- Trakr:自制力差人的计数应用
- 制作后台管理系统首页
- mac 升级java_mac版java更新升级方法
- 计算机组成原理学习 笔记三
- 跑通SpringBoot + dubbo3.0.8 + zookeeper