LeetCode-1631. Path With Minimum Effort-Analysis and Code (Java)

LeetCode-1631. Path With Minimum Effort [Path With Minimum Effort]-Analysis and Code [Java]

1. Topic

You are going to participate in a hike. Give you a two-dimensional rows x columns map heights, where heights[row][col] represents the height of the grid (row, col). At first you are in the top left corner (0, 0), and you want to go to the bottom right corner (rows-1, columns-1) (note that the subscripts are numbered from 0). You can move up, down, left, and right in one of the four directions each time. You want to find the path that consumes the least amount of energy.
The physical energy consumed by a path is determined by the maximum absolute value of the height difference between adjacent grids on the path.
Please return the minimum physical exertion value from the upper left corner to the lower right corner.

Example 1:

输入:heights = [[1,2,2],[3,8,2],[5,3,5]]
输出:2
解释:路径 [1,3,5,3,5] 连续格子的差值绝对值最大为 2 。
这条路径比路径 [1,2,2,2,5] 更优,因为另一条路径差值最大值为 3 。

Example 2:

输入:heights = [[1,2,3],[3,8,4],[5,3,5]]
输出:1
解释:路径 [1,2,3,4,5] 的相邻格子差值绝对值最大为 1 ,比路径 [1,3,5,3,5] 更优。

Example 3:

输入: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]]
输出:0
解释:上图所示路径不需要消耗任何体力。

prompt:

  • rows == heights.length
  • columns == heights[i].length
  • 1 <= rows, columns <= 100
  • 1 <= heights[i][j] <= 106

Source: LeetCode
Link: https://leetcode-cn.com/problems/path-with-minimum-effort The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Two, analysis and code

1. Combine

(1) Thinking

The physical exertion in this question is determined by the maximum height difference, and it can go up, down, left, and right in any direction each time, so it is not suitable to be solved by dynamic programming.
The lattice can be abstracted as a node, and the path can be abstracted as an edge, which can be transformed into a connection problem of the graph. Add edges with smaller height differences in sequence, and record the connectivity between nodes with a union search set. When the starting point and the end point are connected, the height difference of the currently added edge is the answer.

(2) Code

class Solution {
    
    
    public int minimumEffortPath(int[][] heights) {
    
    
        int row = heights.length, col = heights[0].length, len = row * col;
        List<int[]> edges = new ArrayList<int[]>();//存储边,[0]、[1]为节点1、2,[2]为高度差绝对值
        for (int i = 0; i < row; i++) {
    
    
            for (int j = 0; j < col; j++) {
    
    
                int id = i * col + j;
                if (i > 0)//存储向上的边
                    edges.add(new int[]{
    
    id - col, id, Math.abs(heights[i - 1][j] - heights[i][j])});
                if (j > 0)//存储向左的边
                    edges.add(new int[]{
    
    id - 1, id, Math.abs(heights[i][j - 1] - heights[i][j])});
            }
        }
        Collections.sort(edges, new Comparator<int[]>() {
    
    
            public int compare(int[] e1, int[] e2) {
    
    
                return e1[2] - e2[2];
            }
        });

        int [] parent = new int[len];//格子的并查集
        for (int i = 0; i < len; i++)
            parent[i] = i;
        for (int i = 0; i < edges.size();) {
    
    
            int i0 = i;
            while (i < edges.size() && edges.get(i0)[2] == edges.get(i)[2])//添加所有当前高度差的边
                union(parent, edges.get(i)[0], edges.get(i++)[1]);
            if (find(parent, 0) == find(parent, len - 1))
                return edges.get(i0)[2];
        }
        return 0;
    }

    public int find(int[] parent, int i) {
    
    
        if (parent[i] != i)
            parent[i] = find(parent, parent[i]);
        return parent[i];
    }

    public void union(int[] parent, int p1, int p2) {
    
    
        parent[find(parent, p2)] = find(parent, p1);
    }
}

(3) Results

Execution time: 98 ms, beating 57.25% of users
in all Java submissions ; memory consumption: 39.6 MB, beating 21.47% of users in all Java submissions.

Three, other

Nothing.

Guess you like

Origin blog.csdn.net/zml66666/article/details/113797834