题目大意就是,给定一个数组,数组中数字从小到大排列,第一个元素一定是0,青蛙的初始位置就在0,后面依次从小到大排列,表示第几个石子,青蛙只有跳到最后一个石子上才算成功过河,而且青蛙第一次从0位置只能跳一个单位。假如上一次青蛙跳了k个单位,下一次青蛙只能跳 k - 1 , k , k + 1个单位。
这个题的状态不是一个简单的dp数组,因为状态的提取很难用数组实现,这个题要用set和map这种现成的数据结构,这里不再细讲因为这个题只要会C++中的set和map两种存储结构就能简单的AC,map用来储存 i ->value[i] ,set用来储存每个石子上能跳的单位数量。这个题具有明显的递推关系,所以算动态规划类的题目。
 
 bool canCross(vector<int>& stones) {
if(stones[]!=)return false;
set<int>pos1[];
pos1[].insert();
map<int,int>pos2;
int len = stones.size();
for(int i = ;i<len;i++)
pos2[stones[i]] = i;
for(int i = ;i<len;i++){
for(set<int>::iterator j = pos1[i].begin();j!=pos1[i].end();j++){
int tmp = *j;
if(pos2[stones[i]+tmp])pos1[pos2[stones[i]+tmp]].insert(tmp);
if(pos2[stones[i]+tmp+])pos1[pos2[stones[i]+tmp+]].insert(tmp+);
if(pos2[stones[i]+tmp-])pos1[pos2[stones[i]+tmp-]].insert(tmp-);
}
}
return pos1[len-].size();
}

动态规划——Frog Jump的更多相关文章

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

  2. 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: Frog Jump

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

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

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

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

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

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

  9. [leetcode] 403. Frog Jump

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

随机推荐

  1. 【OpenGL】代码记录01创建窗口

    创建空窗口: #include<iostream> // GLEW #define GLEW_STATIC #include <GL/glew.h> // GLFW #incl ...

  2. luogu P5320 [BJOI2019]勘破神机

    传送门 首先我们要知道要求什么.显然每次放方块要放一大段不能从中间分开的部分.设\(m=2\)方案为\(f\),\(m=3\)方案为\(g\),\(m=2\)可以放一个竖的,或者两个横的,所以\(f_ ...

  3. JS判断页面是在浏览器还是微信打开

    一.Navigator对象 1.获取用户的浏览器信息. let ua = navigator.userAgent.toLowerCase(); 打印一下ua的结果: Mozilla/5.0 (Maci ...

  4. 第一天学JAVA,下载JDK,配置JAVA环境变量!!!

    步骤一:下载JDK https://www.oracle.com 我们首先进入到这个网站下载我们运行JAVA的所必须的JDK 进入到这个网站按照图示进入到下一个页面 因为刚学JAVA,我们的JDK版本 ...

  5. MongoDB代码——Python篇

    需要安装的库:pymongo 一.添加文档 from pymongo import MongoClient # 连接服务器 conn = MongoClient("localhost&quo ...

  6. Win2012 R2安装 mysql8.0

    1.官网下载安装 官网上面写着x86,其实是兼容x64和x86的,下载安装就行 2.安装navicat 3.navicat连接mysql的时候出现错误 client does not support ...

  7. springboot格式化时间

    使用@RestController注解,返回的java对象中若含有date类型的属性,则默认输出为TIMESTAMP时间戳格式,可以在配置文件加入下面配置 spring.jackson.date-fo ...

  8. C# - 学习总目录

    C# - 基础 C# - 操作符 C# - 值类型和引用类型 C# - 表达式与语句 C# - 数组 C# - 引用类型 C# - 常用类 C# - 常用接口 C# - LINQ 语言集成查询 C# ...

  9. 【Android手机测试】OOM

    当大多数应用程序都消耗完自己的内存的时候,因为这些应用程序的内存需求加起来超出了物理内存(包括swap)的容量,内核(OOM killer)必须杀掉一些进程才能腾出空间保障系统正常运行 Linux内核 ...

  10. m3u8下载转码一次完成

    最近看到有部分网站开始加入视频解析服务,虽然这种服务会损害土豆优酷等视频托管商的权益,但是,烦人的广告也让我们开始寻找有没有什么比较靠谱的解决方法~实际上很多网站都在使用m3u8文件格式,里面都是视频 ...