Problem solution · Python

Path with Maximum Minimum Value

Path with Maximum Minimum Value: a Python solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Depth-first search
Source
Kamyu LeetCode Solutions
Length
71 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Path with Maximum Minimum Value, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 71 lines of Python from the credited upstream file path-with-maximum-minimum-value.py.
  • The implementation visibly relies on sequence storage, ordered lookup, work queue.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codePath with Maximum Minimum Value · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(m * n * log(m * n))# Space: O(m * n) # binary search + dfs solutionclass Solution(object):    def maximumMinimumPath(self, A):        """        :type A: List[List[int]]        :rtype: int        """        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]                def check(A, val, r, c, lookup):            if r == len(A)-1 and c == len(A[0])-1:                return True            lookup.add((r, c))            for d in directions:                nr, nc = r + d[0], c + d[1]                if 0 <= nr < len(A) and \                   0 <= nc < len(A[0]) and \                   (nr, nc) not in lookup and \                   A[nr][nc] >= val and \                   check(A, val, nr, nc, lookup):                    return True            return False                vals, ceil = [], min(A[0][0], A[-1][-1])        for i in xrange(len(A)):            for j in xrange(len(A[0])):                if A[i][j] <= ceil:                    vals.append(A[i][j])        vals = list(set(vals))        vals.sort()        left, right = 0, len(vals)-1        while left <= right:            mid = left + (right-left)//2            if not check(A, vals[mid], 0, 0, set()):                right = mid-1            else:                left = mid+1        return vals[right]  # Time:  O(m * n * log(m * n))# Space: O(m * n)import heapq  # Dijkstra algorithm solutionclass Solution2(object):    def maximumMinimumPath(self, A):        """        :type A: List[List[int]]        :rtype: int        """        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]        max_heap = [(-A[0][0], 0, 0)]        lookup = set([(0, 0)])        while max_heap:            i, r, c = heapq.heappop(max_heap)            if r == len(A)-1 and c == len(A[0])-1:                return -i            for d in directions:                nr, nc = r+d[0], c+d[1]                if 0 <= nr < len(A) and \                   0 <= nc < len(A[0]) and \                   (nr, nc) not in lookup:                    heapq.heappush(max_heap, (-min(-i, A[nr][nc]), nr, nc))                    lookup.add((nr, nc))            return -1 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗