首页 > 其他分享 >[LeetCode] 1824. Minimum Sideway Jumps

[LeetCode] 1824. Minimum Sideway Jumps

时间:2023-01-21 10:12:06浏览次数:54  
标签:跑道 point int 1824 Jumps obstacles lane Minimum dp

There is a 3 lane road of length n that consists of n + 1 points labeled from 0 to n. A frog starts at point 0 in the second lane and wants to jump to point n. However, there could be obstacles along the way.

You are given an array obstacles of length n + 1 where each obstacles[i] (ranging from 0 to 3) describes an obstacle on the lane obstacles[i] at point i. If obstacles[i] == 0, there are no obstacles at point i. There will be at most one obstacle in the 3 lanes at each point.

  • For example, if obstacles[2] == 1, then there is an obstacle on lane 1 at point 2.

The frog can only travel from point i to point i + 1 on the same lane if there is not an obstacle on the lane at point i + 1. To avoid obstacles, the frog can also perform a side jump to jump to another lane (even if they are not adjacent) at the same point if there is no obstacle on the new lane.

  • For example, the frog can jump from lane 3 at point 3 to lane 1 at point 3.

Return the minimum number of side jumps the frog needs to reach any lane at point n starting from lane 2 at point 0.

Note: There will be no obstacles on points 0 and n.

Example 1:

Input: obstacles = [0,1,2,3,0]
Output: 2 
Explanation: The optimal solution is shown by the arrows above. There are 2 side jumps (red arrows).
Note that the frog can jump over obstacles only when making side jumps (as shown at point 2).

Example 2:

Input: obstacles = [0,1,1,3,3,0]
Output: 0
Explanation: There are no obstacles on lane 2. No side jumps are required.

Example 3:

Input: obstacles = [0,2,1,0,3,0]
Output: 2
Explanation: The optimal solution is shown by the arrows above. There are 2 side jumps.

Constraints:

  • obstacles.length == n + 1
  • 1 <= n <= 5 * 105
  • 0 <= obstacles[i] <= 3
  • obstacles[0] == obstacles[n] == 0

最少侧跳次数。

给你一个长度为 n 的 3 跑道道路 ,它总共包含 n + 1 个 点 ,编号为 0 到 n 。一只青蛙从 0 号点第二条跑道 出发 ,它想要跳到点 n 处。然而道路上可能有一些障碍。

给你一个长度为 n + 1 的数组 obstacles ,其中 obstacles[i] (取值范围从 0 到 3)表示在点 i 处的 obstacles[i] 跑道上有一个障碍。如果 obstacles[i] == 0 ,那么点 i 处没有障碍。任何一个点的三条跑道中 最多有一个 障碍。

比方说,如果 obstacles[2] == 1 ,那么说明在点 2 处跑道 1 有障碍。
这只青蛙从点 i 跳到点 i + 1 且跑道不变的前提是点 i + 1 的同一跑道上没有障碍。为了躲避障碍,这只青蛙也可以在 同一个 点处 侧跳 到 另外一条 跑道(这两条跑道可以不相邻),但前提是跳过去的跑道该点处没有障碍。

比方说,这只青蛙可以从点 3 处的跑道 3 跳到点 3 处的跑道 1 。
这只青蛙从点 0 处跑道 2 出发,并想到达点 n 处的 任一跑道 ,请你返回 最少侧跳次数 。

注意:点 0 处和点 n 处的任一跑道都不会有障碍。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-sideway-jumps
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是动态规划。我们需要一个长度为 3 的数组,分别表示三条跑道,记录到达 dp[i][j] 位置的时候,最小花费是多少。i 表示走到哪一个点了,j 表示在哪一条赛道上。由于走到当前位置只和上一个位置有关,我们可以精简为一维数组。

初始化的时候,因为青蛙是在第二条跑道,跳到隔壁的跑道是有花费的,所以 dp[0] = dp[2] = 1,dp[1] = 0。开始走了之后,看一下当前赛道是否有障碍物,如果没有,则走到当前 i 位置的花费跟上一个位置 i - 1 相同;如果当前位置有障碍,则看一下是否从别的赛道跳过来的花费会更小。

时间O(n)

空间O(1) - 长度只有3的数组

Java实现

 1 class Solution {
 2     public int minSideJumps(int[] obstacles) {
 3         int n = obstacles.length;
 4         int[] dp = new int[3];
 5         // 初始化的时候,0和2位置都是1,因为跳过去会有花费
 6         dp[0] = dp[2] = 1;
 7         for (int i = 1; i < n; i++) {
 8             int object = obstacles[i];
 9             int pre0 = dp[0];
10             int pre1 = dp[1];
11             int pre2 = dp[2];
12             // 注意这里如果填Integer.MAX_VALUE,后面取更小值的时候就不对了
13             Arrays.fill(dp, (int) 2e9);
14 
15             // 如果当前位置无障碍物,那么花费跟上一个位置一样
16             if (object != 1)
17                 dp[0] = pre0;
18             if (object != 2)
19                 dp[1] = pre1;
20             if (object != 3)
21                 dp[2] = pre2;
22             
23             // 看看从隔壁位置跳过来,最小花费是多少
24             if (object != 1)
25                 dp[0] = Math.min(dp[0], Math.min(dp[1], dp[2]) + 1);
26             if (object != 2)
27                 dp[1] = Math.min(dp[1], Math.min(dp[0], dp[2]) + 1);
28             if (object != 3)
29                 dp[2] = Math.min(dp[2], Math.min(dp[0], dp[1]) + 1);
30         }
31         return Math.min(dp[0], Math.min(dp[1], dp[2]));
32     }
33 }

 

LeetCode 题目总结

标签:跑道,point,int,1824,Jumps,obstacles,lane,Minimum,dp
From: https://www.cnblogs.com/cnoodle/p/17063624.html

相关文章

  • CentOS7下配置使用JumpServer 堡垒机 (图文教程)
    前面介绍了如何在《CentOS7下搭建JumpServer堡垒机》,基于这篇文章的环境搭建过程,接着介绍安装后的的功能配置使用。首次wbe登录,https://ip:80,默认账号密码:admin,admin;这......
  • Difference Between Maximum and Minimum Price Sum
    DifferenceBetweenMaximumandMinimumPriceSumThereexistsanundirectedandinitiallyunrootedtreewith$n$nodesindexedfrom$0$to$n-1$.Youaregiv......
  • LeetCode Top 100 Liked Questions 64. Minimum Path Sum (Java版; Medium)
    ​​welcometomyblog​​LeetCodeTop100LikedQuestions64.MinimumPathSum(Java版;Medium)题目描述Givenamxngridfilledwithnon-negativenumbers,fi......
  • JumpServer 对接 Syslog 日志系统
    概述本文章主要介绍JumpServer如何对接Syslog日志系统,并将JumpServer的日志输出到Syslog服务器中。配置测试Syslog服务器:Centos7(关闭iptables/firewalld或者开......
  • JumpServer 如何通过 SFTP 进行文件的上传下载。
    概述本文章主要介绍如何经过JumpServer在SFTP方式下在本机与纳管的Linux服务器之间进行上传下载。登录JumpServer的SFTP页面通过sftp-p2222JMS用户名@JMS登录I......
  • 如何使用 JumpServer 推送资产的系统用户?
    概述本篇文章主要介绍在纳管的资产没有某个系统用户的情况下如何通过JumpServer进行创建并推送到相应的服务器上。文章可以分为Linux系统用户推送和Windows系统用户推......
  • JumpServer 审计录像
    概述本文章主要介绍关于录像文件存放地址、录像保存逻辑、配置外部存储存储录像以及录像文件的播放。一、录像文件存储1、录像文件的存储目录无法修改,仅能调整一级配置目录......
  • VSCode 连接 JumpServer 资产
    概述本文主要介绍如何通过VisualStudioCode(以下简称VSCode)的Remote-SSH插件直连JumpServer所纳管的Linux-SSH协议资产,从而进行远程开发。VSCode是一个运行于Mac......
  • JumpServer 登录密码忘记及用户锁定如何处理
    概述本文主要介绍堡垒机使用过程中产生的密码相关问题。主要包括忘记密码、密码过期、以及登录频繁账号被锁定。忘记密码,密码过期问题描述在使用JumpServer的过程中,可能会......
  • JumpServer & Windows 资产无法连接
    概述本文主要介绍Windows资产突然无法登陆时的情况。详细信息现象:登陆Windows资产时有错误提示。排查思路使用电脑自带的mstsc工具登陆,确认此资产上的用户远程登陆正......