Simple Dijkstra's Algorithm solution with Exp | C++ | Beats 100% | Most optimized
Complexity\n- Time complexity:\nO( M x N log (M x N) )\n- Space complexity:\nO( M x N )\n\n# Code\ncpp []\nclass Solution {\npublic:\n    int minimumTime(vector
# Complexity\n- Time complexity:\nO( M x N log (M x N) )\n- Space complexity:\nO( M x N )\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minimumTime(vector<vector<int>>& grid) {\n\n        if(grid[0][1] > 1 && grid[1][0] > 1)\n        return -1;\n\n        int n = grid.size(), m = grid[0].size();\n\n        int X[4] = {0, 0, -1, 1};\n        int Y[4] = {-1, 1, 0, 0};\n\n        priority_queue< pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>> > pq;\n\n        pq.push({0, {0, 0}});  // time, x, y  .. using Min Heap with respect to time go get minimum time to reach destination .\n\n        vector<vector<int>> visited(n, vector<int> (m , 0)); // to check this element is previously visited or not .\n\n        visited[0][0] = 1; \n\n        while(!pq.empty()){\n            int time = pq.top().first;\n            int currX = pq.top().second.first;\n            int currY = pq.top().second.second;\n            pq.pop();\n            \n            if(currX == n-1 && currY == m-1)\n            return time;\n\n            for(int i=0 ;i<4 ;i++){\n                int newX = currX + X[i];\n                int newY = currY + Y[i];\n\n                if(newX >= 0 && newX < n && newY >= 0 && newY < m && visited[newX][newY] == 0 ){\n                    if(grid[newX][newY] <= time + 1)      \n                    pq.push({time+1, {newX, newY}});\n                    else{\n                        int diff = grid[newX][newY] - time;\n            \n                        if(diff % 2)\n                        pq.push({time + diff, {newX, newY}});\n                        else\n                        pq.push({time + diff + 1 , {newX, newY}});\n                    }  \n                    visited[newX][newY] = 1;  \n                }\n            }\n        }  \n\n        return -1;\n    }\n};\n```
['Breadth-First Search', 'Graph', 'Heap (Priority Queue)', 'Matrix', 'Shortest Path', 'C++']