【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 开始编号)。你每次可以往 上,下,左,右 四个方向之一移动,你想要找到耗费 体力 最小的一条路径。

一条路径耗费的 体力值 是路径上相邻格子之间 高度差绝对值 的 最大值 决定的。

请你返回从左上角走到右下角的最小 体力消耗值 。

解题方法

  1. 采用求最短路径的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用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值