✅ [Python & JavaScript] - Easiest Solutions ✅
Code\npython3 []\nfrom heapq import heappush, heappop\n\nclass Solution:\n    def minimumTime(self, grid: List[List[int]]) -> int:\n        m, n = len(grid), le
# Code\n```python3 []\nfrom heapq import heappush, heappop\n\nclass Solution:\n    def minimumTime(self, grid: List[List[int]]) -> int:\n        m, n = len(grid), len(grid[0])\n        counter = [[float(\'inf\')] * n for _ in range(m)]\n\n        def is_valid(i, j):\n            return 0 <= i < m and 0 <= j < n\n\n        moves = [(-1, 0), (1, 0), (0, 1), (0, -1)]\n\n        heap = []\n        heappush(heap, (0, 0, 0))\n        counter[0][0] = 0\n\n        if grid[0][1] > 1 and grid[1][0] > 1:\n            return -1\n\n        while heap:\n            current, a, b = heappop(heap)\n\n            if (a, b) == (m - 1, n - 1):\n                return current\n\n            for ip, jp in moves:\n                ni, nj = ip + a, jp + b\n                if is_valid(ni, nj):\n                    aux = 0\n                    if current + 1 < grid[ni][nj]:\n                        aux = grid[ni][nj]\n                        if (grid[ni][nj] - current - 1) % 2 != 0:\n                            aux += 1\n                    else:\n                        aux = current + 1\n\n                    if aux < counter[ni][nj]:\n                        heappush(heap, (aux, ni, nj))\n                        counter[ni][nj] = aux\n\n        return -1 if counter[m - 1][n - 1] == float(\'inf\') else counter[m - 1][n - 1]\n```\n``` javascript []\n/**\n * @param {number[][]} grid\n * @return {number}\n */\nvar minimumTime = function(grid) {\n    const m = grid.length;\n    const n = grid[0].length;\n    const counter = Array.from({ length: m }, () => Array(n).fill(Infinity));\n\n    const isValid = (i, j) => i >= 0 && i < m && j >= 0 && j < n;\n\n    const moves = [\n        [-1, 0], [1, 0], [0, 1], [0, -1]\n    ];\n\n    const heap = [];\n    heap.push([0, 0, 0]); // [time, row, col]\n    counter[0][0] = 0;\n\n    if (grid[0][1] > 1 && grid[1][0] > 1) {\n        return -1;\n    }\n\n    while (heap.length > 0) {\n        // Min-heap pop: remove the smallest element\n        heap.sort((a, b) => a[0] - b[0]);\n        const [current, a, b] = heap.shift();\n\n        if (a === m - 1 && b === n - 1) {\n            return current;\n        }\n\n        for (const [ip, jp] of moves) {\n            const ni = a + ip;\n            const nj = b + jp;\n\n            if (isValid(ni, nj)) {\n                let aux = 0;\n                if (current + 1 < grid[ni][nj]) {\n                    aux = grid[ni][nj];\n                    if ((grid[ni][nj] - current - 1) % 2 !== 0) {\n                        aux += 1;\n                    }\n                } else {\n                    aux = current + 1;\n                }\n\n                if (aux < counter[ni][nj]) {\n                    heap.push([aux, ni, nj]);\n                    counter[ni][nj] = aux;\n                }\n            }\n        }\n    }\n\n    return counter[m - 1][n - 1] === Infinity ? -1 : counter[m - 1][n - 1];\n};\n```\n---\n#### Also check out [@GloriousEvolution](https://leetcode.com/u/albrtt/) \'s useful solution.\n---\n
['Array', 'Breadth-First Search', 'Graph', 'Heap (Priority Queue)', 'Matrix', 'Shortest Path', 'Python3', 'JavaScript']