[leetcode] 403. Frog Jump
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的更多相关文章
- [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 ...
- [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 ...
- 第十七周 Leetcode 403. Frog Jump(HARD) 线性dp
leetcode403 我们维护青蛙从某个石头上可以跳那些长度的距离即可 用平衡树维护. 总的复杂度O(n^2logn) class Solution { public: bool canCross( ...
- 【leetcode】403. Frog Jump
题目如下: 解题思路:我的做法是建立一个字典dic,key为stone,value是一个set,里面存的是从前面的所有stone跳跃到当前stone的unit集合.例如stones=[0,1,2,3] ...
- 403 Frog Jump 青蛙过河
一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定 ...
- 403. Frog Jump
做完了终于可以吃饭了,万岁~ 假设从stone[i]无法跳到stone[i+1]: 可能是,他们之间的距离超过了stone[i]所能跳的最远距离,0 1 3 7, 从3怎么都调不到7: 也可能是,他们 ...
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- [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 ...
- 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 ...
随机推荐
- 连载:面向对象葵花宝典:思想、技巧与实践(32) - LSP原则
LSP是唯一一个以人名命名的设计原则,并且作者还是一个"女博士" ======================================================== ...
- JavaScript正则表达式快速判断技巧
这里是JS的正则的一点心得,并不是最完整的规则汇总,更侧重实际运用中的快速判断,初学者接触正则之后往往会被一堆星号括号给弄晕,有了一些速判技巧就能从整体上把握从而不慌乱. JS正则快速判断技巧的核心就 ...
- windows 2003 远程登录时如何修改管理员密码
今天买的vps,需要修改密码.但是自己不会,看网上好多人都说是,按ctrl+alt+del .但是我试过之后发现不对,后来又找到说是使用ctrl+alt+end 更改密码就可以了. 千万不要通过那个 ...
- 2015北京网络赛 H题 Fractal 找规律
Fractal Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingo ...
- hdu2594 Simpsons’ Hidden Talents kmp
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 学习笔记之DB2 9 Fundamentals 730
Sequence中cache的影响,每新建一个连接,next value值增加increment * cache.如果加上order,则会按顺序生成值. increment cache ; Conne ...
- C#获取指定网页源码的几种方法
// WebClient private string GetWebClient(string url) { string strHTML = ""; WebClient myWe ...
- 考试宝典-真题园安卓AppV2.1.0新版发布啦,全新界面,全新体验,全面适配Android 5.0&6.0系统!
真题园移动客户端是真题园网 http://www.zhentiyuan.com 旗下的一款学习考试应用App. 1.全新适配Android5.0.6.0系统,重新优化架构网络通信模块. 2.全新清爽U ...
- Android_listview_scrollListener
layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...
- 深入浅出ECharts系列(一)地图+散点图
深入浅出ECharts系列(一) 目标 本次教程的目标是实现“微博签到点亮中国”散点图,实现结果如图: 2. 准备工作 a) 首先下载ECharts插件,你可以根据自己的实际需求选择你 ...