<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:
        if len(heights) == len(heights[0]) == 1:
            return 0

        # find all possible answers
        possible_answers = set()
        for row in range(len(heights)):
            for col in range(len(heights[0])):
                for child_row, child_col in (
                    (row, col+1),
                    (row+1, col)
                ):
                    if (
                        child_row < len(heights)
                        and child_col < len(heights[0])
                    ):
                        effort = abs(heights[row][col] - heights[child_row][child_col])
                        possible_answers.add(effort)

        # binary search for a path with a given effort
        def path_found(max_effort):
            stack = [(0, 0)]
            seen = {(0, 0)}
            while stack:
                row, col = stack.pop()
                for child_row, child_col in (
                    (row, col-1),
                    (row, col+1),
                    (row-1, col),
                    (row+1, col)
                ):
                    if (
                        0 <= child_row < len(heights)
                        and 0 <= child_col < len(heights[0])
                        and (child_row, child_col) not in seen
                        and abs(heights[row][col] - heights[child_row][child_col]) <= max_effort
                    ):
                        if (
                            child_row == len(heights)-1
                            and child_col == len(heights[0]) - 1
                        ):
                            return True

                        seen.add((child_row, child_col))
                        stack.append((child_row, child_col))

            return False

        possible_answers = sorted(list(possible_answers))
        # print(possible_answers)
        ans = possible_answers[-1]
        left, right = 0, len(possible_answers) - 1
        while left <= right:
            mid = (left + right) // 2
            # print(possible_answers[left], possible_answers[right])
            # print("trying", possible_answers[mid])
            if path_found(possible_answers[mid]):
                ans = possible_answers[mid]
                right = mid - 1
            else:
                left = mid + 1
        
        return ans
19200