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.
Example 1:
[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.
Example 2:
[0,1,2,3,4,8,9,11] Return false. There is no way to jump to the last stone as
the gap between the 5th and 6th stone is too large. 分析:
暴力解法:
首先要明白,不是所有的石头都必须要跳上去,题目没有说清楚,上面的例子也很misleading. 暴力解法很简单,从当前点(石头)和跳到当前点的步数,看后面是否有石头可以reach,如果可以,以那个新的石头和新的步数继续递归,如果我们reach到最后一个石头,return true.
public class Solution {
public boolean canCross(int[] stones) {
if (stones == null || stones.length == ) return true;
if (stones[] - stones[] != ) return false;
return isPossible(stones, , );
}
public boolean isPossible(int[] stones, int start, int jumps) {
if (start == stones.length - )
return true;
for (int i = start + ; i < stones.length; i++) {
int diff = stones[i] - stones[start];
if (diff <= jumps + && diff >= jumps - ) {
if (isPossible(stones, i, diff)) {
return true;
}
}
}
return false;
}
}
方法二:保存跳到某个点的时候的步数,这样,只要最后一个点有步数,我们就可以保证那个点能够被跳到。
public class Solution {
public boolean canCross(int[] stones) {
if (stones == null || stones.length == ) return false;
// Map<position, the # of jumps the frog takes to reach this position>
Map<Integer, Set<Integer>> map = new HashMap<>();
for (int position : stones) {
map.put(position, new HashSet<Integer>());
}
// in order to avoid checking whether the first jump is at position 0 or not,
// we set the # of jumps the frog takes to the first position is 0.
map.get().add();
for (int position : stones) {
for (Integer steps : map.get(position)) {
if (map.containsKey(position + steps - ) && steps - > ) {
map.get(position + steps - ).add(steps - );
}
if (map.containsKey(position + steps) && steps > ) {
map.get(position + steps).add(steps);
}
if (map.containsKey(position + steps + )) {
map.get(position + steps + ).add(steps + );
}
}
}
return map.get(stones[stones.length - ]).size() != ;
}
}
Frog Jump的更多相关文章
- [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: Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [Swift]LeetCode403. 青蛙过河 | 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青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- LeetCode403. 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 青蛙跳
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- div 3 frog jump
There is a frog staying to the left of the string s=s1s2…sn consisting of n characters (to be more p ...
- [leetcode] 403. Frog Jump
https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...
- 403. Frog Jump
做完了终于可以吃饭了,万岁~ 假设从stone[i]无法跳到stone[i+1]: 可能是,他们之间的距离超过了stone[i]所能跳的最远距离,0 1 3 7, 从3怎么都调不到7: 也可能是,他们 ...
随机推荐
- IP欺骗原理与过程分析
IP欺骗攻击法 原创:r00t <r00t@unsecret.org> QQ: 22664566 http://www.unsecret.org --------------------- ...
- Hibernate SQL优化技巧dynamic-insert="true" dynamic-update="true"
最近正在拜读Hibernate之父大作<Java Persistence with Hibernate>,颇有收获.在我们熟悉的Hibernate映射文件中也大有乾坤,很多值得我注意的地方 ...
- NSURLSession & NSCache
用于替代 NSURLConnection 支持后台运行的网络任务 暂停.停止.重启网络任务,不再需要 NSOperation 封装 请求可以使用同样的配置容器 不同的 session 可以使用不同的私 ...
- AngularJS 使用ngOption实现下拉列表
最近使用到了ngOption实现下拉选择列表,由于需要实现分组等功能,百度了下没有太好的文章,就百度到一篇英文的帖子,按照其中的代码很顺利的搞定了. 本篇根据文中代码,详细讲述下如何实现下拉列表 更多 ...
- HTML Table导出为Excel的方法
HTML Table导出为Excel的方法: 直接上源码 <html> <head> <meta http-equiv="Content-Type" ...
- 初探Ajax
1.什么是Ajax Ajax是Asynchronous JavaScript and XML的缩写,这一技术能从服务器请求额外数据而无需卸载页面.传统的HTTP请求流程大概是这样的,浏览器向服务器发送 ...
- eclipse中如何将java项目转为java Web项目
有时候我们在eclipse中导入web项目时,系统当做一个java项目导入进来了.这时候在启动tomcat的服务器时无法找到该项目. 那么可以通过如下操作来将java项目转换为web项目. 1. 选择 ...
- 如何配置和使用Spring框架的bean
1. 首先在src目录下新建beans.xml文件,该文件名可更改. 2. 编辑xml文件如下,这里需要注意的是beans的表头中信息需要根据不同的版本对应的内容不同,本例中使用的spring的版本为 ...
- 详解 iOS 上机题!附个人见解
庸者的救赎2015-11-20 02:30:23 AFN那个使用的时候不需要弱引用的,因为从你的封装方式来看,那个block并不会被当前视图控制器持有,而是被manager持有了,所以不需要__wea ...
- updatepanel用法之triggers(局部刷新,全部刷新)使用示例
asyncpostbacktrigger(异步回调触发器):局部刷新,只刷新updatepanel内部的内容postbacktrigger(普通回调触发器):全部刷新 <asp:ScriptMa ...