当前位置:网站首页>LeetCode 1654. The minimum number of jumps to get home one question per day
LeetCode 1654. The minimum number of jumps to get home one question per day
2022-07-07 16:59:00 【@Little safflower】
Problem description
There's a flea's home on the axis x It's about . Please help it from the position 0 set out , Get to its home .
The rules of flea jumping are as follows :
It can forward Jump just right a A place ( Jump right ).
It can Back up Jump just right b A place ( Jump to the left ).
It can't continuity Jump back 2 Time .
It can't jump to anything forbidden Position in array .
Fleas can jump forward exceed The location of its home , But it is You can't jump to negative integers The location of .Give you an array of integers forbidden , among forbidden[i] It's where fleas can't jump , At the same time give you the whole number a, b and x , Please return to the flea's home for the minimum number of jumps . If it doesn't just arrive x Feasible plan of , Please return -1 .
Example 1:
Input :forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
Output :3
explain : Jump forward 3 Time (0 -> 3 -> 6 -> 9), Fleas are home .
Example 2:Input :forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
Output :-1
Example 3:Input :forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
Output :2
explain : Jump forward once (0 -> 16), And then jump back (16 -> 7), Fleas are home .
Tips :
1 <= forbidden.length <= 1000
1 <= a, b, forbidden[i] <= 2000
0 <= x <= 2000
forbidden All the positions in are different from each other .
Location x be not in forbidden in .source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-jumps-to-reach-home
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int minimumJumps(int[] forbidden, int a, int b, int x) {
// Record the forbidden locations
Set<Integer> set = new HashSet<>();
for(int forbid : forbidden) set.add(forbid);
Queue<int[]> queue = new LinkedList<>();// The current position , Number of backward jumps
queue.offer(new int[]{0,0});
boolean[][] visible = new boolean[8000][2];
int ans = -1;
while(!queue.isEmpty()){
int size = queue.size();
ans++;
for(int i = 0;i < size;i++){
int[] t = queue.poll();
int position = t[0];
if(position == x) return ans;
int rollbackCount = t[1];
if(visible[position][rollbackCount]) continue;
visible[position][rollbackCount] = true;
// Jump forward
if(position + a < 8000 && !set.contains(position + a)){
queue.offer(new int[]{position + a,0});
}
// Jump back
if(position - b >= 0 && !set.contains(position - b) && rollbackCount == 0){
queue.offer(new int[]{position - b,rollbackCount + 1});
}
}
}
return -1;
}
}边栏推荐
- 预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
- Vs2019 configuration matrix library eigen
- 应用在温度检测仪中的温度传感芯片
- Sort out several important Android knowledge and advanced Android development interview questions
- 【DesignMode】代理模式(proxy pattern)
- 掌握这个提升路径,面试资料分享
- A tour of gRPC:03 - proto序列化/反序列化
- QT picture background color pixel processing method
- 谈谈 SAP 系统的权限管控和事务记录功能的实现
- ByteDance Android gold, silver and four analysis, Android interview question app
猜你喜欢
![[Android -- data storage] use SQLite to store data](/img/f6/a4930276b3da25aad3ab1ae6f1cf49.png)
[Android -- data storage] use SQLite to store data

Binary search tree (basic operation)

两类更新丢失及解决办法
![[C language] question set of X](/img/17/bfa57de183c44cf0a3c6637bb65a9d.jpg)
[C language] question set of X
最新Android高级面试题汇总,Android面试题及答案
![[medical segmentation] attention Unet](/img/f4/cf5b8fe543a19a5554897a09b26e68.png)
[medical segmentation] attention Unet

整理几个重要的Android知识,高级Android开发面试题

The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars

二叉搜索树(基操篇)
As an Android Developer programmer, Android advanced interview
随机推荐
爬虫(17) - 面试(2) | 爬虫面试题库
LeetCode 312. 戳气球 每日一题
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
如何快速检查钢网开口面积比是否符合 IPC7525
Inner monologue of accidental promotion
Horizontal and vertical centering method and compatibility
正在准备面试,分享面经
【医学分割】attention-unet
【DesignMode】代理模式(proxy pattern)
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
LeetCode 1043. 分隔数组以得到最大和 每日一题
面试题 01.02. 判定是否互为字符重排-辅助数组算法
一文读懂数仓中的pg_stat
最新Android面试合集,android视频提取音频
蓝桥杯 决赛 异或变换 100分
Opencv personal notes
水平垂直居中 方法 和兼容
ATM系统
网关Gateway的介绍与使用
Binary search tree (basic operation)