题目大意就是,给定一个数组,数组中数字从小到大排列,第一个元素一定是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. svn 解决冲突

    当svn update时提示如下: D C main.go > local file unversioned, incoming file add upon updateSummary of c ...

  2. redis集群配置与管理

    Redis在3.0版本以后开始支持集群,经过中间几个版本的不断更新优化,最新的版本集群功能已经非常完善.本文简单介绍一下Redis集群搭建的过程和配置方法,redis版本是5.0.4,操作系统是中标麒 ...

  3. c++ 常用头文件

    1.#include<iostream> iostream 的意思是输入输出流.#include<iostream>是标准的C++头文件,任何符合标准的C++开发环境都有这个头 ...

  4. 20165337《网络对抗技术》week1 Exp0 Kali安装

    1.下载kali kali官网:https://www.kali.org 在官网中下载,并且在VMvare里打开 2.修改视图 进去之后虚拟机界面很小,需要修改视图来调整 3.网络设置 4.文件夹共享 ...

  5. 清北学堂学习总结day2

    今天是钟皓曦大佬讲课,先来膜一波   %%%%% •数论 数论是这次培训的一个重点,那么什么是数论呢? 数论是研究整数性质的东西,所以理论上day2不会涉及小数QwQ (切入正题) •整除性: 设a, ...

  6. 自己写的一个用js把select换成div与span与ul的东西

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. VS Code保存使用项目Eslint规则格式化代码

    One.文件-首选项-设置-选择项目                  Two.打开右上角JSON设置            Three. 插入以下代码 { "eslint.options& ...

  8. SVM小白教程(1):目标函数

    关于 SVM(支持向量机),网上教程实在太多了,但真正能把内容讲清楚的少之又少.这段时间在网上看到一个老外的 svm 教程,几乎是我看过的所有教程中最好的.这里打算通过几篇文章,把我对教程的理解记录成 ...

  9. 使用SpringSecurity体验OAUTH2之一 (入门1)

    OAUTH2是一种安全的授权框架,其原理在网上有许多文章上可以看到.但从实践角度,好的文章比较少.SpringSecurity框架本身是支持OAUTH2的,所以下面通过使用SpringSecurity ...

  10. 我的redis入门之路

    1:操作环境:vmware12 , centOs7 ,redis5.0.3 centOs7安装与下载链接(原文地址): https://blog.csdn.net/qq_42570879/articl ...