Java🍵✅
Complexity\n- Time complexity: O(n\xD7m\xD7log(n\xD7m))\n\n- Space complexity: O(n\xD7m)\n\n# Code\njava []\nclass Solution {\n    public int minimumTime(int[][
# Complexity\n- Time complexity: **O(n\xD7m\xD7log(n\xD7m))**\n\n- Space complexity: **O(n\xD7m)**\n\n# Code\n```java []\nclass Solution {\n    public int minimumTime(int[][] grid) {\n        if (grid[0][1] > 1 && grid[1][0] > 1)\n            return -1;\n        int n = grid.length;\n        int m = grid[0].length;\n        boolean[][] visited = new boolean[n][m];\n        final int[][] DIRECTIONS = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };\n\n        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));\n        pq.add(new int[] { 0, 0, 0 }); // {time, row, col}\n\n        while (!pq.isEmpty()) {\n            int[] curr = pq.poll();\n            int time = curr[0];\n            int row = curr[1];\n            int col = curr[2];\n\n            if (row == n - 1 && col == m - 1) {\n                return time;\n            }\n\n            if (visited[row][col]) continue;\n                visited[row][col] = true;\n\n                for (int[] direction : DIRECTIONS) {\n                    int newRow = row + direction[0];\n                    int newCol = col + direction[1];\n\n                    if (newRow >= 0 && newCol >= 0 && newRow < n && newCol < m && !visited[newRow][newCol]) {\n                        int waitTime = ((grid[newRow][newCol] - time) % 2 == 0) ? 1 : 0;\n                        int newTime = Math.max(grid[newRow][newCol] + waitTime, time + 1);\n                        pq.add(new int[] { newTime, newRow, newCol });\n                    }\n                }\n            \n        }\n        return -1;\n    }\n}\n\n```
['Breadth-First Search', 'Heap (Priority Queue)', 'Java']