[leetcode]403. Frog Jump青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.
If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.
Note:
- The number of stones is ≥ 2 and is < 1,100.
- Each stone's position will be a non-negative integer < 231.
- The first stone's position is always 0.
[0,1,3,5,6,8,12,17] There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit. Return true. The frog can jump to the last stone by jumping
1 unit to the 2nd stone, then 2 units to the 3rd stone, then
2 units to the 4th stone, then 3 units to the 6th stone,
4 units to the 7th stone, and 5 units to the 8th stone.
题意:
一只不会游泳的青蛙,从位置0th unit(即我画的图中的第0号石头)出发开始跳,
若前一次它跳了K units,
这次就只能跳 K-1 或者 K 或者 K+1 units,
问青蛙是否能跳到对岸。
思路:
以[0,1,3,5,6,8,12,17]为例自己画了个图,我把units理解为直尺上的刻度,更直观理解题意。
用HashMap套一个set,set内存放last jump的units

首先,遍历一遍给定数组stones = {0,1,3,5,6,8,12,17}, 存入所有给定刻度,初始化HashMap。
Integer(当前刻度) 0 1 3 5 6 8 12 17 Set(last jump units) [] [] [] [] [] [] [] []
然后,初始化0刻度对应的Set为[0]。
Integer(当前刻度) 0 1 3 5 6 8 12 17 Set(last jump units) [0] [] [] [] [] [] [] []
最后,遍历一遍给定数组stones = {0,1,3,5,6,8,12,17}
根据题意,青蛙的下一跳(nextJump)范围取决于上一跳(lastJump)
遍历Set里上一跳的取值
Integer(当前刻度) 0 1 3 5 6 8 12 17
Set(last jump units) [0] [] [] [] [] [] [] []
lastJump 0
nextJump -1
0
1
查找map.containsKey(下一跳的刻度),即 (当前刻度 + nextJump Range)
Integer(当前刻度) 0 1 3 5 6 8 12 17
Set(last jump units) [0] [1] [2] [2] [3,1] [3,2] [4] [5]
lastJump 0 1 2 2 3 1 3 2 4 5
nestJump Range:
(1)lastJump-1 -1 0 1 1 2 0 2 1 3
(2)lastJump 0 1 2 2 3 1 3 2 4
(3)lastJump+1 1 2 3 3 4 2 4 3 5
map.containsKey 0-1✗ 1+0✗ 3+1✗ 5+1✓ 6+2✓ 6+0✗ 8+2✗ 8+1✗ 12+3✗
0+0✗ 1+1✗ 3+2✓ 5+2✗ 6+3✗ 6+1✗ 8+3✗ 8+2✗ 12+4✗
0+1✓ 1+2✓ 3+3✓ 5+3✓ 6+4✗ 6+2✓ 8+4✓ 8+3✗ 12+5✓
代码:
public boolean canCross(int[] stones) {
if (stones.length == 0) return false;
HashMap<Integer, HashSet<Integer>> map = new HashMap<>();
for (int i = 0; i < stones.length; i++) {
map.put(stones[i], new HashSet<>());
}
map.get(0).add(0);
for (int i = 0; i < stones.length; i++) {
for (int lastJump : map.get(stones[i])) {
for (int nextJump = lastJump - 1; nextJump <= lastJump + 1; nextJump++) {
if (nextJump > 0 && map.containsKey(stones[i] + nextJump)) {
map.get(stones[i] + nextJump).add(nextJump);
}
}
}
}
return !map.get(stones[stones.length - 1]).isEmpty();
}
[leetcode]403. Frog Jump青蛙过河的更多相关文章
- [LeetCode] 403. Frog Jump 青蛙跳
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- 403 Frog Jump 青蛙过河
一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定 ...
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [leetcode] 403. Frog Jump
https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...
- 第十七周 Leetcode 403. Frog Jump(HARD) 线性dp
leetcode403 我们维护青蛙从某个石头上可以跳那些长度的距离即可 用平衡树维护. 总的复杂度O(n^2logn) class Solution { public: bool canCross( ...
- 【JavaScript】Leetcode每日一题-青蛙过河
[JavaScript]Leetcode每日一题-青蛙过河 [题目描述] 一只青蛙想要过河. 假定河流被等分为若干个单元格,并且在每一个单元格内都有可能放有一块石子(也有可能没有). 青蛙可以跳上石子 ...
- 【leetcode】403. Frog Jump
题目如下: 解题思路:我的做法是建立一个字典dic,key为stone,value是一个set,里面存的是从前面的所有stone跳跃到当前stone的unit集合.例如stones=[0,1,2,3] ...
- 403. Frog Jump
做完了终于可以吃饭了,万岁~ 假设从stone[i]无法跳到stone[i+1]: 可能是,他们之间的距离超过了stone[i]所能跳的最远距离,0 1 3 7, 从3怎么都调不到7: 也可能是,他们 ...
- Java实现 LeetCode 403 青蛙过河
403. 青蛙过河 一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中. 给定石子的位置列表(用单元格序 ...
随机推荐
- make menuconfig时出现 #include CURSES_LOC错误
In :: scripts/kconfig/lxdialog/:: fatal error: curses.h: 没有那个文件或目录 #include CURSES_LOC ^ compilation ...
- 2018ICPC网络赛(焦作站)K题题解
一.题目链接 https://nanti.jisuanke.com/t/31720 二.题意 给$N$种船只,第$i$种船的载重量是$V_i$,数量是$2^{C_i}-1$.接下来有$Q$次询问,每次 ...
- javascript节点操作replaceChild()
replaceChild(a,b)是用来替换文档中的已有元素的 参数a:要插入的节点, 参数b:要替换的节点 var oDiv = document.getElementById("guoD ...
- 第10课 C++中的新成员
1. 动态内存分配 (1)C++通过new关键字进行动态内存申请,是以类型为单位来申请空间大小的 (2)delete关键字用于内存释放 ▲注意释放数组时要加[],否则只释放这个数组中的第1个元素. [ ...
- 【Python编程:从入门到实践】chapter9 类
chapter9 类 9.1 创建和使用类 9.1.1 创建Dog类 class Dog(): """一次模拟小狗的简单尝试""" def ...
- Spring Security编程模型
1.采用spring进行权限控制 url权限控制 method权限控制 实现:aop或者拦截器(本质就是之前之后进行控制)--------------------proxy就是 2.权限模型: 本质理 ...
- php使用ffmpeg向视频中添加文字字幕
这篇文章主要介绍了PHP使用ffmpeg给视频增加字幕显示的方法,实例分析了php操作ffmpeg给视频增加字母的技巧,具有一定参考借鉴价值,需要的朋友可以参考下. 本文实例讲述了PHP使用ffmpe ...
- PyCharm License Activation激活码失效问题的解决方法
自己的小Python项目好几天没有写了,今天打开PyCharm准备继续写,突然发现之前的激活码被取消不能用了,本来激情满满的准备干活啦!之前搜的这个激活码本来说的是可以用到18年1月份的呢,哎…心情是 ...
- windows2008r2共享文件夹设置方法
一,无法启用网络发现的方法 参考网站: http://www.jb51.net/os/windows/win2008/154631.html Function Discovery R ...
- JS实现让滚轮控制网页头部显示与隐藏
在很多网站中都有鼠标网上滚动头部就会滑出,继续往下滚动就会隐藏,下面看看实现方法 scroll(); function scroll(){// 入口方法 这个方法是获取事件的兼容,获取delta -- ...