LeetCode 3342: Find Minimum Time to Reach Last Room II Solution

Master LeetCode problem 3342 (Find Minimum Time to Reach Last Room II), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3342. Find Minimum Time to Reach Last Room II

Problem Explanation

Explanation

To solve this problem, we can use Dijkstra's algorithm to find the shortest path from the starting room to the last room. We can create a priority queue to store the rooms based on the total time taken to reach that room. We start from the starting room (0, 0) with time t = 0 and add it to the priority queue. Then, we iteratively process the rooms from the priority queue, updating the time taken to reach each adjacent room and adding it to the queue. We continue this process until we reach the last room (n-1, m-1).

Algorithm

  1. Initialize a priority queue to store rooms based on the total time taken to reach them.
  2. Create a 2D array to store the minimum time taken to reach each room, initialized with infinity.
  3. Add the starting room (0, 0) with time t = 0 to the priority queue.
  4. While the priority queue is not empty:
    • Extract the room with the minimum time from the priority queue.
    • Update the time taken to reach each adjacent room based on the current room's time.
    • Add the adjacent rooms to the priority queue if the updated time is less than the previously calculated time.
  5. Return the minimum time taken to reach the last room (n-1, m-1).

Time Complexity

The time complexity of this algorithm is O(NMlog(N*M)), where N is the number of rows and M is the number of columns in the grid.

Space Complexity

The space complexity of this algorithm is O(N*M) for storing the minimum time to reach each room.

Solution Code

import java.util.*;

class Solution {
    public int minTimeToReachLastRoom(int[][] moveTime) {
        int n = moveTime.length;
        int m = moveTime[0].length;
        
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);
        int[][] minTime = new int[n][m];
        for (int i = 0; i < n; i++) {
            Arrays.fill(minTime[i], Integer.MAX_VALUE);
        }
        
        pq.offer(new int[]{0, 0, 0});
        int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        
        while (!pq.isEmpty()) {
            int[] curr = pq.poll();
            int x = curr[0];
            int y = curr[1];
            int time = curr[2];
            
            if (x == n - 1 && y == m - 1) {
                return time;
            }
            
            for (int[] dir : dirs) {
                int newX = x + dir[0];
                int newY = y + dir[1];
                if (newX >= 0 && newX < n && newY >= 0 && newY < m) {
                    int newTime = time + (time % 2 == 0 ? 1 : 2) + moveTime[newX][newY];
                    if (newTime < minTime[newX][newY]) {
                        minTime[newX][newY] = newTime;
                        pq.offer(new int[]{newX, newY, newTime});
                    }
                }
            }
        }
        
        return -1; // Should not reach here
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3342 (Find Minimum Time to Reach Last Room II)?

This page provides optimized solutions for LeetCode problem 3342 (Find Minimum Time to Reach Last Room II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3342 (Find Minimum Time to Reach Last Room II)?

The time complexity for LeetCode 3342 (Find Minimum Time to Reach Last Room II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3342 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3342 in Java, C++, or Python.

Back to LeetCode Solutions