Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

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:
Leetcode: Path With Maximum Minimum Value

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:
Leetcode: Path With Maximum Minimum Value

Input: [[2,2,1,2,2,2],[1,2,2,2,1,2]]
Output: 2

Example 3:
Leetcode: Path With Maximum Minimum Value

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. 1 <= R, C <= 100
  2. 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
}
linkedin
github
slack

Post Views: 0
Posted in MediumTagged #graph, unionfind

Post navigation

LeetCode: Connecting Cities With Minimum Cost
LeetCode: Immediate Food Delivery I

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.