1. 程式人生 > 其它 >【Lintcode】1418. Path With Maximum Minimum Value

【Lintcode】1418. Path With Maximum Minimum Value

技術標籤:# DFS、BFS與圖論演算法javaleetcode資料結構

題目地址:

https://www.lintcode.com/problem/path-with-maximum-minimum-value/description

給定一個 m × n m\times n m×n的二維矩陣 A A A,找到從 A [ 0 ] [ 0 ] A[0][0] A[0][0] A [ m − 1 ] [ n − 1 ] A[m-1][n-1] A[m1][n1]的路徑,使得路徑上的最小值最大。路徑每一步可以走四個方向,但同一個格子不能重複走。返回該路徑的最小值。

一個比較好理解也比較好寫的方法是用並查集來做。思路參考

https://blog.csdn.net/qq_46105170/article/details/109713256。思路非常像Kruskal最小生成樹演算法。程式碼如下:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    
    class UnionFind {
        private int[] parent;
        
        public UnionFind(int size) {
            parent =
new int[size]; Arrays.fill(parent, -1); } public void insert(int x) { parent[x] = x; } public int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return
parent[x]; } public void union(int x, int y) { int px = find(x), py = find(y); if (px == py) { return; } parent[px] = py; } public boolean exists(int x) { return parent[x] != -1; } } class Pair { int x, y, val; public Pair(int x, int y, int val) { this.x = x; this.y = y; this.val = val; } } /** * @param A: a List[List[int]] * @return: Return the maximum score of a path */ public int maximumMinimumPath(int[][] A) { // Write your code here int m = A.length, n = A[0].length; UnionFind uf = new UnionFind(m * n); List<Pair> list = new ArrayList<>(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { list.add(new Pair(i, j, A[i][j])); } } int start = 0, end = m * n - 1; int[] d = {1, 0, -1, 0, 1}; // 按權值從大到小排序 list.sort((p1, p2) -> -Integer.compare(p1.val, p2.val)); for (int i = 0; i < list.size(); i++) { Pair pair = list.get(i); int x = pair.x, y = pair.y; uf.insert(transfer(x, y, n)); for (int j = 0; j < 4; j++) { int nextX = x + d[j], nextY = y + d[j + 1]; if (0 <= nextX && nextX < m && 0 <= nextY && nextY < n && uf.exists(transfer(nextX, nextY, n))) { uf.union(transfer(x, y, n), transfer(nextX, nextY, n)); } } if (uf.exists(start) && uf.exists(end) && uf.find(start) == uf.find(end)) { return pair.val; } } return -1; } private int transfer(int x, int y, int n) { return x * n + y; } }

時間複雜度 O ( m n log ⁡ ( m n ) ) O(mn\log (mn)) O(mnlog(mn)),空間 O ( m n ) O(mn) O(mn)