题目大意就是,给定一个数组,数组中数字从小到大排列,第一个元素一定是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. JGUI源码:实现简单MVVM单项绑定学习笔记(15)

    前面几节都是jquery界面方面的东西,本节研究些数据方面的东西:MVVM. MVVM由三部分组成:Model <=> ViewModel <=> View,当Model数据改 ...

  2. docker学习------centos7.5下的swarm集群可视化构建

    1.swarm集群 manager : 192.168.211.175 agent1    : 192.168.211.176    agent2    :  192.168.211.177 2.环境 ...

  3. Unknown system variable 'query_cache_size'] with root cause

    Unknown system variable 'query_cache_size'] with root cause 出现这个错误是因为mysql连接数据库的版本不对, mysql-connecto ...

  4. 从XML文件和properties文件提取数据

    XML文档格式内容如下 <?xml version="1.0" encoding="UTF-8"?> <root>     <fi ...

  5. linux --xampp 配置多个网站

    我们想要在本地安装两个测试域名,www.abc.tld, www.xyz.tld, 分别指向到 htdoc 目录下的 abc.tld 和 xyz.tld 文件夹下.tld 是顶级域名 the top ...

  6. window C/C++ 简单的IDE编译器

    C-Free 官网链接: http://www.programarts.com/cfree_ch/download.htm

  7. Django 2.0 官方文档翻译

    from django.contrib import admin from django.urls import include, path urlpatterns = [ path('polls/' ...

  8. JavaScript—对象创建方式

    JavaScript 也是面向对象的语言(oop) 之前学JavaScript 没有学对象.现在做下笔记 创建对象的方式: 1.  对象字面量 const hero = { name: '吕布', w ...

  9. WebRTC Precompiled 使用

    最近研究webrtc native code,但源码太大(10GB以上)又需要FQ,就找了个预编译的版本https://sourcey.com/precompiled-webrtc-libraries ...

  10. python&JSONP(初级篇)

    JSONP产生背景 1.跨域的安全限制都是对浏览器端来说的,服务器端是不存在跨域安全限制的. 2.浏览器的同源策略限制从一个源加载的文档或脚本与来自另一个源的资源进行交互. 3.如果协议,端口和主机对 ...