JAVA SOLUTION
Intuition\n\nThe solution uses a priority queue (min-heap) to handle the time constraints for each cell. Each cell in the grid has a minimum time it becomes acc
# Intuition\n\nThe solution uses a priority queue (min-heap) to handle the time constraints for each cell. Each cell in the grid has a minimum time it becomes accessible, and if you arrive before that time, you must wait. The priority queue ensures that we always process the cell with the least time first, exploring paths efficiently. For each cell, we check its neighbors and update their times, considering the waiting time if necessary. By using BFS with the priority queue and a visited array, the algorithm ensures we find the minimum time to reach the bottom-right cell.\n\n# Approach\n\nThe approach uses a modified Dijkstra\'s algorithm with a priority queue to handle the time constraints for each cell in the grid:\n\nPriority Queue: We use a priority queue (min-heap) to always process the cell with the smallest time first, ensuring an optimal traversal of the grid.\n\nTime Calculation: Each cell in the grid has a minimum required time for access. As we move to neighboring cells, we ensure that the time we arrive at a cell is greater than or equal to the cell\'s required time. If not, we wait until it\'s accessible.\n\nBFS-like Traversal: Starting from the top-left corner, we explore the grid in four directions (up, down, left, right). For each neighboring cell, we calculate the earliest possible time we can visit it (considering both the time spent moving and any waiting time if necessary).\n\nVisited Array: We use a visited array to track the minimum time we\'ve reached each cell to avoid revisiting with a worse time.\n\nTermination: The process continues until we reach the bottom-right cell, and the time at that point is the minimum time required. If we can\'t reach the bottom-right cell, we return -1.\n\nThis approach ensures that we efficiently calculate the minimum time while respecting the waiting constraints at each cell.\n\n# Complexity\n- Time complexity:O((n\xD7m)\u22C5log(n\xD7m))\n\n\n- Space complexity:O(n\xD7m)\n\n\n# Code\n```java []\nclass Pair {\n    int row, col, time;\n    Pair(int row, int col, int time) {\n        this.row = row;\n        this.col = col;\n        this.time = time;\n    }\n}\n\nclass Solution {\n    public int minimumTime(int[][] grid) {\n        if(grid[0][1] > 1 && grid[1][0] > 1) return -1;\n\n        int n = grid.length;\n        int m = grid[0].length;\n        int[][] dirs = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n        boolean[][] visited = new boolean[n][m];\n        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));\n        pq.offer(new int[]{grid[0][0], 0, 0});\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            if (visited[row][col]) continue;\n            visited[row][col] = true;\n\n            for (int[] dir : dirs) {\n                int r = row + dir[0];\n                int c = col + dir[1];\n                if (r < 0 || r >= n || c < 0 || c >= m || visited[r][c]) continue;\n                int wait = (grid[r][c] - time) % 2 == 0 ? 1 : 0;\n                pq.offer(new int[]{Math.max(grid[r][c] + wait, time + 1), r, c});\n            }\n        }\n        return -1;\n    }\n}\n\n\n```
['Array', 'Breadth-First Search', 'Heap (Priority Queue)', 'Matrix', 'Java']