Skip to content

3628. Find Minimum Time To Reach Last Room Ii

Difficulty: Medium

LeetCode Problem View on GitHub


3628. Find Minimum Time to Reach Last Room II

Medium


There is a dungeon with n x m rooms arranged as a grid.

You are given a 2D array moveTime of size n x m, where moveTime[i][j] represents the minimum time in seconds when you can start moving to that room. You start from the room (0, 0) at time t = 0 and can move to an adjacent room. Moving between adjacent rooms takes one second for one move and two seconds for the next, alternating between the two.

Return the minimum time to reach the room (n - 1, m - 1).

Two rooms are adjacent if they share a common wall, either horizontally or vertically.

 

Example 1:

Input: moveTime = [[0,4],[4,4]]

Output: 7

Explanation:

The minimum time required is 7 seconds.

  • At time t == 4, move from room (0, 0) to room (1, 0) in one second.
  • At time t == 5, move from room (1, 0) to room (1, 1) in two seconds.

Example 2:

Input: moveTime = [[0,0,0,0],[0,0,0,0]]

Output: 6

Explanation:

The minimum time required is 6 seconds.

  • At time t == 0, move from room (0, 0) to room (1, 0) in one second.
  • At time t == 1, move from room (1, 0) to room (1, 1) in two seconds.
  • At time t == 3, move from room (1, 1) to room (1, 2) in one second.
  • At time t == 4, move from room (1, 2) to room (1, 3) in two seconds.

Example 3:

Input: moveTime = [[0,1],[1,2]]

Output: 4

 

Constraints:

  • 2 <= n == moveTime.length <= 750
  • 2 <= m == moveTime[i].length <= 750
  • 0 <= moveTime[i][j] <= 109

Solution

class Solution {
    public int minTimeToReach(int[][] moveTime) {
        int n = moveTime.length;
        int m = moveTime[0].length;
        PriorityQueue<long[]> pq = new PriorityQueue<>((a, b) -> Long.compare(a[2], b[2]));
        long[][][] dist = new long[n][m][2];
        for (long curr[][] : dist) for (long curr1[] : curr) Arrays.fill(curr1, Long.MAX_VALUE / 10);
        pq.offer(new long[]{0, 0, 0, 0});
        dist[0][0][0] = 0;
        int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        while (!pq.isEmpty()) {
            long[] current = pq.poll();
            long x = current[0];
            long y = current[1];
            long time = current[2];
            long x1 = current[3];
            if (x == n - 1 && y == m - 1) return (int)(time);
            for (int[] dire : dir) {
                int newX = (int)(x + dire[0]);
                int newY = (int)(y + dire[1]);
                if (newX >= 0 && newX < n && newY >= 0 && newY < m) {
                    long to_add = 0;
                    if (x1 == 1) to_add = 2;
                    else to_add = 1;
                    long current1 = (long)(Math.max(time, moveTime[newX][newY])) + to_add;
                    if (current1 < dist[newX][newY][(int)x1]) {
                        dist[newX][newY][(int)x1] = current1;
                        pq.offer(new long[]{newX, newY, current1, 1 - x1});
                    }
                }
            }
        }
        return -1;
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Approach

Detailed explanation of the approach will be added here