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. SQL函数中的动态执行语句

    一.为什么要使用动态执行语句? 由于在PL/SQL 块或者存储过程中只支持DML语句及控制流语句,并不支持DDL语句,所以Oracle动态执行语句便应允而生了.关于DDL与DML的区别,请参见:DDL ...

  2. 【转】Netty那点事(四)Netty与Reactor模式

    [原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch4-reactor.md 一:Netty.NIO.多线程? 时 ...

  3. 如何使用validate.js进行动态添加和移除表单验证信息

    表单是我们在开当中的常客,那么对表单的验证也是必须的,那么如何实现动态给表单添加验证规则呢? 方法: 1,动态添加验证规则 // 添加$("#addConnectUser").ru ...

  4. 常见排序算法及其java实现

    最近学习了下java,感觉java在基本语法上与C++非常相似.作为练习,我用java实现了冒泡排序.选择排序.插入排序.基尔排序.快速排序.堆排序.计数排序.合并排序. 以下为实现代码: publi ...

  5. 【转】bt协议的详细分析

    转自:https://baoz.net/bt-protocol/ 一 BT系统的组成结构 1 普通的Web服务器:        例如Apache或IIS服务器 2 一个静态的种子文件:     即. ...

  6. android学习日记22--Animation动画简介

    Animation动画主要有两种:帧动画(Frame Animation)和补间动画(Tween Animation).补间动画主要包括对位置.角度.尺寸等属性的变化,而帧动画则是通过若干帧图片轮流切 ...

  7. 获取用户ip接口

    <script type="text/javascript" charset="gb2312" src="http://counter.sina ...

  8. [Effective C++ --010]令赋值操作符返回一个reference to *this

    差不多最经典的就是这个了: x = y = z = ; 解读为: x = (y = ( z = )); 如果没有返回值,上述代码就不能通过编译. 其实看到标题就差不多明白这一条了,但是为什么连续赋值时 ...

  9. PAT 1017

    1017. Queueing at Bank (25) Suppose a bank has K windows open for service. There is a yellow line in ...

  10. How to Tune Java Garbage Collection--reference

    reference:http://architects.dzone.com/articles/how-tune-java-garbage The Performance Zone is support ...