Dijkstra
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:

    def setval(self):
        return 10**9

    def dijkstra(self, i, j, grid):
        from collections import defaultdict
        import heapq
        n, m = len(grid), len(grid[0])
        queue = [(0, i, j)]
        dist = defaultdict(self.setval)
        dist[(i, j)] = 0
        while queue:
            d, i, j = heapq.heappop(queue)
            pairs = [(i, min(j + 1, m - 1)), (i, max(j - 1, 0)), (min(i + 1, n - 1), j), (max(i - 1, 0), j)]
            for x, y in pairs:
                val = grid[x][y]
                k = 0 if (val - d) % 2 else 1
                num = 1 if d + 1 >= val else val + k - d 
                if d + num < dist[(x, y)]:
                    dist[(x, y)] = d + num
                    heapq.heappush(queue, (dist[(x, y)], x, y))
        return dist[(n - 1, m - 1)]
        
    
    def minimumTime(self, grid: list[list[int]]) -> int:
        n, m = len(grid), len(grid[0])
        if grid[min(1, n - 1)][0] > 1 and grid[0][min(1, m - 1)] > 1:
            return -1
        return self.dijkstra(0, 0, grid)
```
['Graph', 'Heap (Priority Queue)', 'Shortest Path', 'Python3']