[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 ...
随机推荐
- iOS开发Block的使用
Block 是从 iOS4引入的,在日常开发中,会经常用到Block.特别是在多线程中,Block的用处更广泛.而且,Block不仅可以接收参数,其本身也可以作为参数,因此,Block的功能非常强大. ...
- SQLyog MySQL GUI 11.13 Ultimate 中文破解版【转载】
SQLyog是一个易于使用的.快速而简洁的图形化管理MYSQL数据库的工具,它能够在任何地点有效地管理你的数据库! SQLyog MySQL GUI是我常用的一个桌面工具,功能强大,让你有使用MSSQ ...
- Java连接MYSQL【转载】
这篇文章主要以MySQL为例讲下Java如何连接到数据库的. 当然,首先要安装有JDK(一般是JDK1.5.X).然后安装MySQL,这些都比较简单,具体过程就不说了.配置好这两个环境后,下载JDBC ...
- BZOJ 1090: [SCOI2003]字符串折叠 区间DP
1090: [SCOI2003]字符串折叠 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- tomcat的几种部署方法
原文:http://blog.163.com/pg_roger/blog/static/185884389201172921211937/ 1.将应用文件夹或war文件(均指编译好的文件)塞到tomc ...
- 文件和目录之设置用户ID和设置组ID
与一个进程相关联的ID有6个或更多,它们如表4-4所示: 表4-4 与每个进程相关联的用户ID和组ID 实际用户ID 我们实际上是谁 实际组ID ...
- PAT 1008
1008. Elevator (20) The highest building in our city has only one elevator. A request list is made u ...
- cocos2dx 公告效果
第一种方法: http://blog.csdn.net/jackystudio/article/details/12991977 [玩转cocos2d-x之十六]滚动字幕和公告 第二种方法: http ...
- Lipo Error!! can't open input file
参考文章:http://stackoverflow.com/questions/17348912/lipo-error-cant-open-input-file I got it to Work, i ...
- Cummins INSITE locked and ask for verification code
Some Cummins INSITE users turn to our engineer with a same question: INSITE has detected an invalid ...