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. Java foreach操作(遍历)数组

    语法: 我们分别使用 for 和 foreach 语句来遍历数组 运行结果: 练习: import java.util.Arrays; public class HelloWorld { public ...

  2. linux添加ssh用户

    正好有朋友问,就转过来分享下. 转自:http://blog.sina.com.cn/s/blog_6fc583e70100n6rm.html 测试环境:CentOS 5.5 1.添加用户,首先用ad ...

  3. Spring Quartz结合Spring mail定期发送邮件

    文件配置例如以下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  4. C#-获取datatable指定列的数据

    DataTable dt = new DataTable();            da.Fill(dt);                                    this.text ...

  5. Print2flash在.NET(C#)64位中的使用,即文档在线预览

    转:http://www.cnblogs.com/flowwind/p/3411106.html Print2flash在.NET(C#)中的使用,即文档在线预览   office文档(word,ex ...

  6. 信号之sigsuspend函数

    更改进程的信号屏蔽字可以阻塞所选择的信号,或解除对它们的阻塞.使用这种技术可以保护不希望由信号中断的代码临界区.如果希望对一个信号解除阻塞,然后pause等待以前被阻塞的信号发生,则又将如何呢?假定信 ...

  7. C#_MVC_分页update

    private static string getLinkHtml(UrlHelper urlHelper, bool useAjax, string ajaxSuccessFunction, str ...

  8. input的多条数据以数组形势上传

    <input type="text" name="prices[]" value="">

  9. Java ZIP File Example---refernce

    In this tutorial we are going to see how to ZIP a file in Java. ZIP is an archive file format that e ...

  10. 深入理解Binder(二),Binder是什么?

    上篇文章深入理解Binder(一),从AIDL谈起我们介绍了AIDL的基本使用,用AIDL两个App的通信是实现了,可是又有小伙伴疑惑了,为什么使用AIDL就能够实现两个App之间的通信?本文我们就来 ...