https://leetcode.com/contest/5/problems/frog-jump/

这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边,一会找一下这道题(找到了,就是这个55. Jump Game)。然后这道题,差不多是相同的意思,但是每次只能跳3个或者2个,跳了之后还要判断那个位置有没有石头,然后还要记录上一次跳的间隔,然后我就想到了要用map做一个位置到index的映射,主要用于o(1)的查找,可以用unordered_map来做,然后每个位置还要记录上一次跳的间隔,但是不用记住上一次的位置,考虑可以多次转移,然后每一个位置用set记录,上次的间隔。最后想一下,数据范围也就1000,应该很容易的就过了,就这样。

 class Solution {
public:
bool canCross(vector<int>& s) {
int n = s.size();
if(n == ) return ;
if(s[] + != s[]) return ;
vector<bool> res(n + , );
res[] = ;
set<int> se;
set<int> a[n];
map<int, int> m;
for (int i = ; i < n; i++) {se.insert(s[i]);
m[s[i]] = i;
}
a[].insert();
for (int i = ; i < n - ; i++) {
for (auto it = a[i].begin(); it != a[i].end(); it++) {
//cout << i << " " << *it << endl;
int cur = *it;
if(cur - > ) {
if(m.count(s[i] + cur - )) {
int t = m[s[i] + cur - ];
a[t].insert(cur - );
res[t] = ;
}
}
if(m.count(s[i] + cur)) {
int t = m[s[i] + cur];
a[t].insert(cur);
res[t] = ;
}
if(m.count(s[i] + cur + )) {
int t = m[s[i] + cur + ];
a[t].insert(cur + );
res[t] = ;
} }
}
return res[n - ];
}
};

[leetcode] 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(HARD) 线性dp

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

  4. 【leetcode】403. Frog Jump

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

  5. 403 Frog Jump 青蛙过河

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

  6. 403. Frog Jump

    做完了终于可以吃饭了,万岁~ 假设从stone[i]无法跳到stone[i+1]: 可能是,他们之间的距离超过了stone[i]所能跳的最远距离,0 1 3 7, 从3怎么都调不到7: 也可能是,他们 ...

  7. 【一天一道LeetCode】#55. Jump Game

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

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

  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. @@IDENTITY与SCOPE_IDENTITY的用法

    SCOPE_IDENTITY   和   @@IDENTITY   的作用都是取得返回在当前会话中的任何表内所生成的最后一个标识值,简单的说就是在执行一条插入语句之后使用@@IDENTITY的全局变量 ...

  2. JAVA js的escape函数、解析用js encodeURI编码的字符串、utf8转gb2312的函数

    在使用webView时,如果url中参数有中文的话,拦截到的字符串就会类似这样的:http://api.letstar.cn/zq/news.html?id=20&cupName=%E6%B5 ...

  3. Eclipse+Maven构建web项目及部署时Maven lib依赖问题的解决

    目录 Eclipse中m2e插件构建web项目的步骤 Maven工具构建web项目再导入Eclipse的步骤 [一].Eclipse中m2e插件构建web项目的步骤 第一步:创建项目,按照 New – ...

  4. 使用CATransition实现页面的“从左向右” “从右向左”的动画

    -(void)initView{ UISwipeGestureRecognizer *left_gesture=[[UISwipeGestureRecognizer alloc]initWithTar ...

  5. PowerShell中的数学计算

    Double类型和float都属于浮点类型,精度不高.而Decimal属于高精度

  6. 2015北京网络赛 G题 Boxes bfs

    Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...

  7. materialish-progress

    https://github.com/pnikosis/materialish-progress materialish-progress-master.zip

  8. UITableViewCell 高度自适应

    UITableViewCell 高度自适应一直是我们做动态Cell高度时遇到的最烦躁的问题,Cell动态高度计算可以去看看sunny的这篇文章介绍,今天主要和大家分享下我在使用systemLayout ...

  9. 【转】Android开发之Bitmap的内存优化详解

    本文来源:转载自: http://mobile.51cto.com/abased-410796.htm 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitma ...

  10. h5拖放-ff的bug

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...