<p>You are a hiker preparing for an upcoming hike. You are given <code>heights</code>, a 2D array of size <code>rows x columns</code>, where <code>heights[row][col]</code> represents the height of cell <code>(row, col)</code>. You are situated in the top-left cell, <code>(0, 0)</code>, and you hope to travel to the bottom-right cell, <code>(rows-1, columns-1)</code> (i.e.,&nbsp;<strong>0-indexed</strong>). You can move <strong>up</strong>, <strong>down</strong>, <strong>left</strong>, or <strong>right</strong>, and you wish to find a route that requires the minimum <strong>effort</strong>.</p>

<p>A route&#39;s <strong>effort</strong> is the <strong>maximum absolute difference</strong><strong> </strong>in heights between two consecutive cells of the route.</p>

<p>Return <em>the minimum <strong>effort</strong> required to travel from the top-left cell to the bottom-right cell.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex1.png" style="width: 300px; height: 300px;" /></p>

<pre>
<strong>Input:</strong> heights = [[1,2,2],[3,8,2],[5,3,5]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex2.png" style="width: 300px; height: 300px;" /></p>

<pre>
<strong>Input:</strong> heights = [[1,2,3],[3,8,4],[5,3,5]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex3.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> This route does not require any effort.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>rows == heights.length</code></li>
	<li><code>columns == heights[i].length</code></li>
	<li><code>1 &lt;= rows, columns &lt;= 100</code></li>
	<li><code>1 &lt;= heights[i][j] &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution:
    def minimumEffortPath(self, heights: List[List[int]]) -> int:
        # guide solution = idea is that if you can make it with X effort, and you can make it with
        # all effort below x, and if you cannot make it with x effort, then you cannot make it
        # with any effort above x
        # can use binary search on effort from 1 to the max of the values (since it is all possible)

        def opt(heights):
            mini = 0 # min of range of efforts
            maxi = 0 # max of range of efforts
            for r in heights: # O(row*col)
                for c in r:
                    maxi = max(maxi, c)
            
            # function to check if it is possible with effort k
            def check(heights, k):
                # can use dfs starting from top left to bottom right
                def dfs(heights, x, y, k, seen):
                    m = len(heights)
                    n = len(heights[0])
                    # check if it is to move to neighbors with effort k
                    for i, j in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
                        if 0 <= i < m and 0 <= j < n and (i,j) not in seen:
                            diff = abs(heights[x][y] - heights[i][j])
                            if diff <= k:
                                seen.add((i,j))
                                dfs(heights, i, j, k, seen)

                seen = set()
                seen.add((0,0))
                m = len(heights)
                n = len(heights[0])
                dfs(heights, 0, 0, k, seen)
                return (m-1, n-1) in seen

            while mini < maxi:
                mid = (mini+maxi)//2
                if check(heights, mid):# if it is possible with this effort, check if any lower effort is possible
                    maxi = mid
                else:
                    mini = mid + 1
            return mini # mini will contain the leftmost valid answer

        # brute force - each cell has 4 choices and 4 directions that they
        # can come from = O(4^n)
        # state is cell and which direction we came from
        def bfs(heights0):

            UP = 0
            DOWN = 1
            LEFT = 2
            RIGHT = 3
            m = len(heights)
            n = len(heights[0])
            target = (m-1, n-1)

            seen = set() # set of states we already seen (direction we came from, cell we are at)
            seen.add((UP, (0,0)))

            def getNeighbors(heights, x,y, m, n, curDiff):
                ret = []
                v = heights[x][y]
                for f, c in [(DOWN, (x-1,y)), (UP, (x+1,y)), (RIGHT,(x,y-1)), (LEFT,(x,y+1))]:
                    i, j = c
                    if 0 <= i < m and 0 <= j < n:
                        diff = abs(v-heights[i][j])
                        ret.append((f,c,max(curDiff, diff)))
                return ret

            dq = deque()
            dq.append((UP, (0,0), 0))
            ans = float("inf")
            while dq:
                dirFrom, coords, maxDiff = dq.popleft()
                x, y = coords
                if coords == target:
                    ans = min(ans, maxDiff)
                for neighbor in getNeighbors(heights, x, y, m, n, maxDiff):
                    if neighbor not in seen:
                        seen.add(neighbor)
                        dq.append(neighbor)
            return ans
        return opt(heights)
21300