做完了终于可以吃饭了,万岁~

假设从stone[i]无法跳到stone[i+1]:

可能是,他们之间的距离超过了stone[i]所能跳的最远距离,0 1 3 7, 从3怎么都调不到7;

也可能是,他们之间的距离小于stone[i]能跳的最近距离,0 1 3 6 10 11,从10无法挑到11;

那么有没有可能下一个石头到当前的距离,小于最大值,大于最小值,因为中间有空白让我正好跳不到?(突然发现自己变成青蛙了)

我没用严谨的数学公式证明,但是写了几个式子发现不存在这种情况,如果出现,那么在i之前就判断失败了。

所以动态规划就要一个最大值,一个最小值:

max[i]表示在石头stone[i]上,最远能跳多远;

min[i]就是最近跳多近。

最小的距离要从最近的石头开始找,stone[i-1],stone[i-2]..一旦找到就停止。

判断最近的石头能不能跳过来,就是开始说的石头之间的距离<=max[i-1] && >= min[i-1]。跳过来那么最短距离就是当前俩石头之间的距离diff-1,减1是因为下次跳是k-1,k,k+1。

最大的距离就是从最远的石头stone[1]开始找,一旦成功就停止。

假设有一块石头能跳到当前,那么上面2个值就都存在,就是那块石头到当前石头的距离-1 +1;一块都没有,max[i] min[i]就都是初始的值,Java里面是0.

一但出现max[i] == 0 min[i] == 0就说明跳不到。一直遍历到最后就行了。

代码速度AC了,也是没仔细想,肯定能改进,比如中间的找最大最小可以用二分之类的。。

public class Solution {
public boolean canCross(int[] stones)
{
if(stones.length == 1) return true;
if(stones[1] != 1) return false; int[] max = new int[stones.length];
int[] min = new int[stones.length];
max[0] = 1;min[0] = 1;
max[1] = 2;min[1] = 1; for(int i = 2; i < stones.length;i++)
{
int L = 1;
int R = i-1;
int target = 0; //find mini
while(R > 1)
{
target = stones[i] - stones[R];
if(max[R] >= target && min[R] <= target)
{
min[i] = Math.max(target - 1,1);
break;
}
else R--;
}
while(L <= R)
{
target = stones[i] - stones[L];
if(max[L] >= target && min[L] <= target)
{
max[i] = target + 1;
break;
}
else L++;
}
if(max[i] == 0 && min[i] == 0) return false; } return true; }
}

感觉难在用数学式子证明如果小于最大,大于最小一定有解,但是我数学太差,就不丢人了。

403. Frog Jump的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [leetcode] 403. Frog Jump

    https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...

  4. 第十七周 Leetcode 403. Frog Jump(HARD) 线性dp

    leetcode403 我们维护青蛙从某个石头上可以跳那些长度的距离即可 用平衡树维护. 总的复杂度O(n^2logn) class Solution { public: bool canCross( ...

  5. 403 Frog Jump 青蛙过河

    一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定 ...

  6. 【leetcode】403. Frog Jump

    题目如下: 解题思路:我的做法是建立一个字典dic,key为stone,value是一个set,里面存的是从前面的所有stone跳跃到当前stone的unit集合.例如stones=[0,1,2,3] ...

  7. [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 ...

  8. Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  9. 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 ...

随机推荐

  1. 设计模式之 State 状态模式

    状态模式的核心在于 1. 状态的转换导致行为(Handle)的差异,比如人的状态是饿的时候,吃(Handle)的行为是2个馒头,人状态是不太饿的时候,吃(Handle)的行为是半个馒头 2. Stat ...

  2. jquery中mouseout和mouseleave 事件的区别

    今天用jQuery写了一个选项卡的效果,用mouseout事件控制了鼠标的移出,结果发现在移出时div会发生闪动,于是网上各种查资料觉得用mouseleave更合适一些,

  3. angularjs入门整理

    之前发过一篇博文,从mobile angular ui的demo和其官网初识整个angularjs的大体使用,但是没很好学习,只是通过一些技术博文初步认识,陷入很多坑.所以现在在中文官网正式整理下知识 ...

  4. VPN ,Bypass the FIrewall

    Bypass the China Firewall Methods November 16th, 2012Posted in , Tech With their assortment of techn ...

  5. Python 关于正负无穷float(‘inf’)的一些用法

    Python中可以用如下方式表示正负无穷: float("inf"), float("-inf") 利用 inf 做简单加.乘算术运算仍会得到 inf > ...

  6. 使用pyinstaller 2.1将python打包并添加版本信息和图标

    最近用 wxpython写了一个小的脚本,因为想要发布给没有装python和wxpython的人使用,遂决定使用pyinstaller 2.1进行打包. 其中遇到几个问题: 1,给打包的文件添加图标 ...

  7. Python: 设计模式 之 工厂模式例(1)

    #!/usr/bin/env python #coding=utf-8 # # 工厂模式一例 # 版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126) ...

  8. gnuplot使用

    直接用yum安装gnuplot即可,例如 sudo sh -c "yum install gnuplot.x86_64 " 安装以后就可以使用了 编写gnuplot脚本 # grp ...

  9. gcd 控制线程执行顺序(供参考)

    dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, dispatch_get_global_qu ...

  10. 前端图片预览,上传前预览,兼容IE7、8、9、10、11,Firefox,Chrome(学习到的知识)

    文章地址:http://www.cnblogs.com/rubylouvre/p/4597344.html 一.window.URL 在Chrome中,window.URL和window.webkit ...