博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LintCode] Frog Jump
阅读量:6309 次
发布时间:2019-06-22

本文共 3094 字,大约阅读时间需要 10 分钟。

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 - 1k, or k + 1 units. Note that the frog can only jump in the forward direction.

 Notice
  • The number of stones is ≥ 2 and is < 1100.
  • Each stone's position will be a non-negative integer < 2^31.
  • The first stone's position is always 0.
Example
Given stones = [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.Given stones = `[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.

这道题这个题意很难懂,其实是说第一个点是0, 第二个点是1,然后下面开始,每一次都可以跳上一次多跳的步数k/k+1/k-1步 eg. 0 -> 1 k = 0 + 1   1 -> 3 k = 1 + 1 (3 - 2 =1)   3 -> 5 k = 2 + 0   5 -> 8 k = 2 + 1   8 -> 12 k = 3 + 1   12 -> 17 k = 4 + 1 思路一: 动态规划,想到动态规划是因为这是求状态的问题,同时给定的数据顺序有关系。需要注意的是下一次可以跳的步数,直接与上一次能跳的步数有关。在这里面我们设计一个map,其实key是stone的值,value是一个set,里面是存了该值可以前进的步数。 然后我们依次循环除了最后的一个stones,每一个stones都拿出来得到它可以前进的步数,在这个stone的基础上加上步数(k/k+1/k-1),如果可以跳到其中的一个stone,我们需要把这个步数存下来,因为下一次这个点就可以在这个的基础上跳(k/k+1/k-1) 最后就是看最后一个stone的set是不是空的,如果是空的就说明前面没有石头能跳到这一步。
public class Solution {    /*     * @param stones: a list of stones' positions in sorted ascending order     * @return: true if the frog is able to cross the river or false     */    public boolean canCross(int[] stones) {        // write your code here        if (stones == null || stones.length == 0) {            return false;        }                Map
> dp = new HashMap
>(); for (int i = 0; i < stones.length; i++) { dp.put(stones[i], new HashSet
()); } dp.get(0).add(0); for (int i = 0; i < stones.length - 1; i++) { int stone = stones[i]; for (Integer k : dp.get(stone)) { if (k - 1 > 0 && dp.containsKey(stone + k - 1)) dp.get(stone + k - 1).add(k - 1); if (dp.containsKey(stone + k)) dp.get(stone + k).add(k); if (dp.containsKey(stone + k + 1)) dp.get(stone + k + 1).add(k + 1); } } return !dp.get(stones[stones.length - 1]).isEmpty(); }}

思路二

就是用dfs来做,因为可以看成不停的搜素,先所有的点都用k+1来试,不行就用k,然后k-1。

http://blog.csdn.net/YABAJ/article/details/77115969

转载于:https://www.cnblogs.com/codingEskimo/p/8404523.html

你可能感兴趣的文章
并查集模板
查看>>
RESTful Mongodb
查看>>
BZOJ3237:[AHOI2013]连通图(线段树分治,并查集)
查看>>
如何提高Ajax性能
查看>>
Android--自定义加载框
查看>>
LINUX下 lamp安装及配置
查看>>
BZOJ3105 [cqoi2013]新Nim游戏
查看>>
困惑的前置操作与后置操作
查看>>
SDNU 1269.整数序列(水题)
查看>>
BZOJ 2118 Dijkstra
查看>>
Go语言基础之结构体
查看>>
SpringCloud:Eureka Client项目搭建(Gradle项目)
查看>>
jqueryValidate
查看>>
ATL使用IE控件,并且屏蔽右键
查看>>
Jenkins
查看>>
linux下使用screen和ping命令对网络质量进行监控
查看>>
数据库设计技巧
查看>>
css定位概述
查看>>
C# 动态修改配置文件 (二)
查看>>
BOM:文档对象模型 --树模型
查看>>