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
```cpp []
class Solution {
public:
    vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int minimumTime(vector<vector<int>>& grid) {
        int m=grid.size();
        int n=grid[0].size();
        if(grid[1][0]>grid[0][0]+1 and grid[0][1]>grid[0][0]+1){
            return -1;
        }
        vector<vector<int>> distance(m, vector<int>(n, INT_MAX));
        priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
        pq.push({0, {0, 0}});
        distance[0][0]=0;
        while(pq.size()>0){
            int time = pq.top().first;
            int i = pq.top().second.first;
            int j = pq.top().second.second;
            pq.pop();
            for(auto dir : directions){
                int new_i = i+dir.first;
                int new_j = j+dir.second;
                if(new_i>=0 and new_i<m and new_j>=0 and new_j<n){
                    int n_time=0;
                    if(time+1<grid[new_i][new_j]){
                        n_time=grid[new_i][new_j];
                        int diff=grid[new_i][new_j]-time-1;
                        n_time = diff%2==0?n_time:n_time+1;
                    }
                    else{
                        n_time=time+1;
                    }

                    if(n_time<distance[new_i][new_j]){
                        distance[new_i][new_j]=n_time;
                        pq.push({n_time, {new_i, new_j}});
                    }
                }
            }
        }
        return distance[m-1][n-1]==INT_MIN?-1:distance[m-1][n-1];


    }
};



```
['Heap (Priority Queue)', 'C++']