Easiest Sol Java || Beats 100% ✅ || Simple Dijkstra's Algorithm
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves navigating a grid where each cell has a time constraint representi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves navigating a grid where each cell has a **time constraint** representing the earliest time you can enter it. The goal is to find the **minimum time** to reach the bottom-right corner while respecting these constraints.\n\nThe challenge comes from needing to **wait** when trying to enter a cell too early and ensuring the path is the fastest possible. The intuition is similar to **Dijkstra\'s algorithm** for finding the shortest path in a weighted graph, where:\n1. The grid cells are the nodes.\n2. Moving between adjacent cells is equivalent to edges with weights of 1 (plus any waiting time).\n3. The priority queue processes nodes (cells) in increasing order of their entry time.\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Grid Initialization**:\n   - Treat the grid as a graph where each cell is a node.\n   - Use a **priority queue** (min-heap) to always process the next cell with the smallest entry time.\n\n2. **Handling Time Constraints**:\n   - For each neighboring cell, calculate the time it will take to reach it.\n   - If the cell\'s constraint (`grid[row][col]`) requires waiting, calculate the **additional time needed** to align with its accessibility.\n\n3. **Breadth-First Search with Priority Queue**:\n   - Start from `(0, 0)` at time `0`.\n   - Process neighboring cells, updating the minimum time to reach them and adding them to the priority queue.\n   - Stop processing when you reach the destination `(rows-1, cols-1)`.\n\n4. **Edge Cases**:\n   - If the starting cell\'s neighbors are inaccessible early on, return `-1`.\n   - If no valid path exists (queue is exhausted without reaching the destination), return `-1`.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n1. **Grid Traversal**:\n   - Each cell is processed at most once, and every move explores up to 4 neighbors. This results in \\(O(R \\times C \\times 4)\\), where \\(R\\) and \\(C\\) are the number of rows and columns.\n\n2. **Priority Queue Operations**:\n   - Inserting and removing an element in the priority queue takes \\(O(\\log(R \\times C))\\), and there are at most \\(R \\times C\\) cells to process.\n   - Total complexity for priority queue operations is \\(O((R \\times C) \\log(R \\times C))\\).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n1. **Priority Queue**:\n   - Stores at most \\(R \\times C\\) cells.\n   - Space complexity: \\(O(R \\times C)\\).\n\n2. **Visited Array**:\n   - A 2D array `seen` of size \\(R \\times C\\) is used to track visited cells.\n   - Space complexity: \\(O(R \\times C)\\).\n\n# Code\n```java []\nclass Solution {\n    private static final int[][] MOVES = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};\n    \n    public int minimumTime(int[][] grid) {\n        int rows = grid.length;\n        int cols = grid[0].length;\n        \n        if (grid[0][1] > 1 && grid[1][0] > 1) {\n            return -1;\n        }\n        \n        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); \n        boolean[][] seen = new boolean[rows][cols];\n        \n        pq.offer(new int[]{0, 0, 0}); // time, row, col\n        seen[0][0] = true;\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            for (int[] dir : MOVES) {\n                int newRow = row + dir[0];\n                int newCol = col + dir[1];\n                \n                if (newRow < 0 || newRow >= rows || \n                    newCol < 0 || newCol >= cols || \n                    seen[newRow][newCol]) {\n                    continue;\n                }\n                \n                int newTime = time + 1;\n                if (grid[newRow][newCol] > newTime) {\n                    int wait = ((grid[newRow][newCol] - newTime + 1) / 2) * 2;\n                    newTime += wait;\n                }\n                \n                if (newRow == rows - 1 && newCol == cols - 1) {\n                    return newTime;\n                }\n                \n                seen[newRow][newCol] = true;\n                pq.offer(new int[]{newTime, newRow, newCol});\n            }\n        }\n        \n        return -1;\n    }\n}\n```
['Array', 'Breadth-First Search', 'Graph', 'Heap (Priority Queue)', 'Matrix', 'Shortest Path', 'Java']