首页 > 其他分享 >LeetCode 3341. Find Minimum Time to Reach Last Room I

LeetCode 3341. Find Minimum Time to Reach Last Room I

时间:2024-11-13 22:56:47浏览次数:1  
标签:Last room int 3341 moveTime cur time dp Room

原题链接在这里: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 }

 

标签:Last,room,int,3341,moveTime,cur,time,dp,Room
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18545017

相关文章

  • 推荐一个Elasticsearch ES可视化客户端工具:ES-King
    ES-King:开源免费,一个现代、实用的ESGUI客户端,支持多平台。下载地址:https://github.com/Bronya0/ES-King功能清单详尽的集群信息:节点信息、堆内存占用、总内存占用、cpu占用、磁盘占用、网络流量、节点角色、集群健康、5分钟负载、每个节点的字段缓存、段缓存、查询缓存、请求......
  • centos7安装elasticsearch:7.9.3
    服务器安装elasticsearch:7.9.3一、安装前准备检查系统环境:确保CentOS7系统已经更新到最新版本。检查系统的硬件资源,确保满足Elasticsearch的安装和运行要求。安装OpenJDK:Elasticsearch需要Java环境,这里选择安装OpenJDK11。使用命令sudoyuminstalljava-11-open......
  • Elasticsearch简介
    前言什么是搜索引擎搜索引擎是指根据一定的策略、运用特定的计算机程序从互联网上采集信息,在对信息进行组织和处理后,为用户提供检索服务,将检索的相关信息展示给用户的系统。分类:全文索引搜索引擎采集ip段内的网页数据,扫描网页内容的每一个词,对其创建索引,指明词......
  • ElasticSearch 7.14 向已启用XPACK认证的集群增加新的节点
    一、环境现状描述:     目前的ElasticSearch集群仅有一个单一节点,且这个集群中已建立有索引,索引已包含业务文档数据(超过200G),该集群已经启用XPACK认证,现希望扩展这个集群,增加复制节点,且复制节点启动后,自动从主节点同步数据到新节点。     目前的ElasticSearch集群节点......
  • SpringBoot+ElasticJob实现分布式任务调度
    目录1相关简介2Zookeeper的Docker安装3Zookeeper的Windows版本安装4Zookeeper图形化客户端prettyZoo5示例代码6添加任务监听器7参考资料(感谢)1相关简介zookeeper:开源分布式应用程序协调服务下载地址:https://archive.apache.org/dist/zookeeper/2Zookeeper......
  • SpringBoot项目引入Elasticsearch时启动失败
    1、前情提要:https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/installation.html以上是Elasticsearch对接Java的官方文档(pom依赖部分)我本地Windows安装的Elasticsearch也是8.15.3版本 2、启动报错***************************APPLICATION......
  • ELK的ElasticStack安装
    目录传送门前言一、组件概念各组件概念EFKELKELFKELFK+kafka二、下载地址三、window下7.8版本安装单机四、window下7.8版本安装集群五、Linux下7.8版本安装单机1、ES安装2、ES-HEAD安装3、IK中文分词器安装六、Linux下7.8版本安装集群传送门SpringMVC的源码解析(精......
  • Elasticsearch上创建的index是yellow健康状态的解决方案
    在Elasticsearch中,索引的健康状态(healthstatus)反映了索引的分片分配情况和集群的整体健康状况。这些状态可以帮助您快速了解索引和集群的运行情况。以下是Elasticsearch中索引的三种健康状态及其意义:1.green(绿色)含义:所有主分片(primaryshards)和副本分片(replicashards)都已成功......
  • macOS 下使用 Docker 安装 ElasticSearch(学习环境用)
    当前环境操作系统:macOS15.0.1Docker版本:DockerDesktop:Version4.34.3(170107)DockerEngine:27.2.0安装步骤提示:此部署只为学习使用,没有挂载本地文件1、安装ElasticSearch#安装命令#1.1创建网络somenetwork用于docker间通讯dockernetworkcreateso......
  • roomGPT - 用 AI 打造专属你的梦幻房间
    更多AI开源软件:AI开源-小众AIhttps://www.aiinn.cn/sourcesroomGPT使用了ML模型以及ControlNet来生成各式各样的房间,他会根据你上传的房间照片,自动生成一张你“梦幻房间”的图片。​​主要功能上传房间图片:用户可以通过上传实际拍摄的房间照片或3D渲染图,RoomGPT......