LeetCode 3341: Find Minimum Time to Reach Last Room I Solution

Master LeetCode problem 3341 (Find Minimum Time to Reach Last Room I), 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.

3341. Find Minimum Time to Reach Last Room I

Problem Explanation

Explanation:

To solve this problem, we can use Dijkstra's algorithm. We can treat each room as a node in a graph and connect adjacent rooms with edges. The weight of each edge will be the time required to move from one room to another. We start from the room (0, 0) and find the shortest path to reach the room (n-1, m-1) using Dijkstra's algorithm.

Algorithm:

  1. Create a priority queue to store rooms based on their minimum time to reach.
  2. Initialize a distance array to store the minimum time to reach each room. Initialize all distances as infinite except for the starting room which is set to 0.
  3. Add the starting room to the priority queue.
  4. While the priority queue is not empty, do the following:
    • Extract the room with the minimum time from the priority queue.
    • For each adjacent room, update its distance if the current path through the extracted room is shorter.
    • Add the adjacent rooms to the priority queue if their distances are updated.
  5. Return the distance to the destination room (n-1, m-1).

Time Complexity:

The time complexity of this approach is O(nmlog(n*m)), where n and m are the dimensions of the dungeon grid.

Space Complexity:

The space complexity is O(n*m) for storing the distances and the priority queue.

:

Solution Code

import java.util.*;

class Solution {
    public int findMinimumTime(int[][] moveTime) {
        int n = moveTime.length;
        int m = moveTime[0].length;
        
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);
        int[][] dist = new int[n][m];
        for (int i = 0; i < n; i++) {
            Arrays.fill(dist[i], Integer.MAX_VALUE);
        }
        dist[0][0] = 0;
        pq.offer(new int[]{0, 0, 0});
        
        int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        
        while (!pq.isEmpty()) {
            int[] curr = pq.poll();
            int x = curr[0], y = curr[1], time = curr[2];
            if (x == n - 1 && y == m - 1) {
                return time;
            }
            for (int[] dir : dirs) {
                int nx = x + dir[0];
                int ny = y + dir[1];
                if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                    int newTime = time + 1 + moveTime[nx][ny];
                    if (newTime < dist[nx][ny]) {
                        dist[nx][ny] = newTime;
                        pq.offer(new int[]{nx, ny, newTime});
                    }
                }
            }
        }
        
        return -1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3341 (Find Minimum Time to Reach Last Room I)?

This page provides optimized solutions for LeetCode problem 3341 (Find Minimum Time to Reach Last Room I) 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 3341 (Find Minimum Time to Reach Last Room I)?

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

Can I run code for LeetCode 3341 on DevExCode?

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

Back to LeetCode Solutions