[leetCode]最小体力消耗路径

题目

https://leetcode-cn.com/problems/path-with-minimum-effort/

在这里插入图片描述

二分查找

题目可以转化为:
是否存在一条路径,该路径上的体力值不超过x,可以从左上角到达右下角
假设x = x0时存在路径可以从左上角到达右下角,那么当x增大时原来的路径仍然可以使用。因此可以使用二分查找,每次估测一个x,然后进行广度或者深度优先搜索,最后根据能否到达右下角来缩小搜索范围。

class Solution {

    private int[][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    public int minimumEffortPath(int[][] heights) {
        int rows = heights.length;
        int cols = heights[0].length;
        int left = 0, right = 999999;
        int ans = 0;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            boolean[][] seen = new boolean[rows][cols]; 
            Queue<int[]> queue = new LinkedList<>();
            queue.offer(new int[]{0, 0});
            seen[0][0] = true;
            while (!queue.isEmpty()) {
                int[] cell = queue.poll();
                int x = cell[0], y = cell[1];
                for (int[] dir : dirs) {
                    int nx = x + dir[0];
                    int ny = y + dir[1];
                    if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && !seen[nx][ny] 
                    && Math.abs(heights[x][y] - heights[nx][ny]) <= mid) {
                        queue.offer(new int[]{nx, ny});
                        seen[nx][ny] = true;
                    }
                }
            }
            if (seen[rows - 1][cols - 1]) {
                ans = mid;
                right = mid - 1; 
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
}

并查集

将这 rows * cols 个节点放入并查集中,实时维护它们的连通性。

由于我们需要找到从左上角到右下角的最短路径,因此我们可以将图中的所有边按照权值从小到大进行排序,并依次加入并查集中。当我们加入一条权值为 x 的边之后,如果左上角和右下角从非连通状态变为连通状态,那么 x 即为答案。

class Solution {
    public int minimumEffortPath(int[][] heights) {
        int rows = heights.length;
        int cols = heights[0].length;
        List<int[]> edges = new ArrayList<>();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                int id = i * cols + j;
                if (i > 0) {
                    edges.add(new int[]{id - cols, id, Math.abs(heights[i][j] - heights[i - 1][j])});
                }
                if (j > 0) {
                    edges.add(new int[]{id - 1, id, Math.abs(heights[i][j] - heights[i][j - 1])});
                }
            }
        }
        Collections.sort(edges, (o1, o2)-> o1[2]- o2[2]);
        UnionFind uf = new UnionFind(rows * cols);
        int ans = 0;
        for (int[] edge : edges) {
            int x = edge[0], y = edge[1], v = edge[2];
            uf.union(x, y);
            if (uf.connected(0, rows * cols - 1)) { // (r - 1) * r + c - 1
                ans = v;
                break;
            }
        }
        return ans;
    }
    
    class UnionFind {
        private int[] parent;

        public UnionFind (int n) {
            parent = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
            }
        } 

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

        public void union(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) 
                return;
            parent[rootX] = rootY;
        }

        public boolean connected(int x, int y) {
            return find(x) == find(y);
        }
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值