LeetCode 3341. Find Minimum Time to Reach Last Room I

原题链接在这里:https://leetcode.com/problems/find-minimum-time-to-reach-last-room-i/description/

题目:

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 exactly one second.

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: 6

Explanation:

The minimum time required is 6 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 one second.

Example 2:

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

Output: 3

Explanation:

The minimum time required is 3 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 one second.
  • At time t == 2, move from room (1, 1) to room (1, 2) in one second.

Example 3:

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

Output: 3

Constraints:

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

题解:

To find the minimum time, we can think of using BFS. But here, we need to sort based the arriveTime, thus we use minHeap.

In the minHeap, we need to maintain the currrent coordinates x, y and time.

We also need a 2D array dp to track the minimum time to arrive at each coordinates.

If we reach the last room, return the current time.

Otherwise, we use current time + wait time + 1 and check if it is smaller than the dp arriveTime. If yes, update dp arriveTime and add to the queue.

Time Complexity: O(mnlogmn). m = moveTime.length. n = moveTime[0].length.

Space: O(mn).

AC Java:

 1 class Solution {
 2     public int minTimeToReach(int[][] moveTime) {
 3         if(moveTime == null || moveTime.length == 0 || moveTime[0].length == 0){
 4             return -1;
 5         }
 6 
 7         int m = moveTime.length;
 8         int n = moveTime[0].length;
 9         int[][] dp = new int[m][n];
10         for(int i = 0; i < m; i++){
11             Arrays.fill(dp[i], Integer.MAX_VALUE);
12         }
13 
14         PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[2] - b[2]);
15         dp[0][0] = 0;
16         minHeap.add(new int[]{0, 0, 0});
17         int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
18         while(!minHeap.isEmpty()){
19             int[] cur = minHeap.poll();
20             if(cur[0] == m - 1 && cur[1] == n - 1){
21                 return cur[2];
22             }
23 
24             for(int[] dir : dirs){
25                 int x = cur[0] + dir[0];
26                 int y = cur[1] + dir[1];
27                 if(x < 0 || x >= m || y < 0 || y >= n){
28                     continue;
29                 }
30 
31                 int waitTime = Math.max(0, moveTime[x][y] - cur[2]);
32                 int arriveTime = cur[2] + 1 + waitTime;
33                 if(arriveTime < dp[x][y]){
34                     dp[x][y] = arriveTime;
35                     minHeap.add(new int[]{x, y, arriveTime});
36                 }
37             }
38         }
39 
40         return -1;
41     }
42 }

 

posted @ 2024-11-13 23:07  Dylan_Java_NYC  阅读(9)  评论(0编辑  收藏  举报