LeetCode: Path With Maximum Minimum Value Posted on August 29, 2019July 26, 2020 by braindenny Path With Maximum Minimum Value Similar Problems: CheatSheet: Leetcode For Code Interview CheatSheet: Common Code Problems & Follow-ups Tag: #graph, #unionfind Given a matrix of integers A with R rows and C columns, find the maximum score of a path starting at [0,0] and ending at [R-1,C-1]. The score of a path is the minimum value in that path. For example, the value of the path 8 -> 4 -> 5 -> 9 is 4. A path moves some number of times from one visited cell to any neighbouring unvisited cell in one of the 4 cardinal directions (north, east, west, south). Example 1: Input: [[5,4,5],[1,2,6],[7,4,6]] Output: 4 Explanation: The path with the maximum score is highlighted in yellow. Example 2: Input: [[2,2,1,2,2,2],[1,2,2,2,1,2]] Output: 2 Example 3: Input: [[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0],[4,6,5,4,3]] Output: 3 Note: 1 <= R, C <= 100 0 <= A[i][j] <= 10^9 Github: code.dennyzhang.com Credits To: leetcode.com Leave me comments, if you have better ways to solve. Solution: // https://code.dennyzhang.com/path-with-maximum-minimum-value // Basic Ideas: unionfind // // Sort values. Keep adding big values until src and dst is connected // // Complexity: Time O(nm*log(nm)), Space O(nm) import "sort" type UF struct { parent []int } func constructor(size int) UF { parent := make([]int, size) for i, _ := range parent { parent[i] = i } return UF{parent:parent} } func (uf *UF) union(i, j int) { uf.parent[uf.find(j)] = uf.find(i) } func (uf *UF) find(x int) int{ l := []int{} for x != uf.parent[x] { l = append(l, x) x = uf.parent[x] } for _, v := range l { uf.parent[v] = x } return x } func maximumMinimumPath(A [][]int) int { l := [][]int{} visited := make([][]bool, len(A)) for i, row := range A { visited[i] = make([]bool, len(A[0])) for j, v := range row { l = append(l, []int{v, i, j}) } } // reverse sort.Slice(l, func(i, j int) bool { return l[i][0] > l[j][0] }) totalcnt := len(A)*len(A[0]) uf := constructor(totalcnt) for _, node := range l { v, i, j := node[0], node[1], node[2] visited[i][j] = true for _, offset := range [][]int{[]int{1, 0}, []int{-1, 0}, []int{0, 1}, []int{0, -1}} { i2, j2 := i+offset[0], j+offset[1] if i2>=0 && i2<len(A) && j2>=0 && j2<len(A[0]) { if visited[i2][j2] { uf.union(i*len(A[0])+j, i2*len(A[0])+j2) if uf.find(0) == uf.find(totalcnt-1) { return v } } } } } return -1 } Post Views: 0