第十七周 Leetcode 403. Frog Jump(HARD) 线性dp
我们维护青蛙从某个石头上可以跳那些长度的距离即可 用平衡树维护。
总的复杂度O(n^2logn)
class Solution {
public:
bool canCross(vector<int>& stones) {
map<int,int>po;
int n=stones.size();
map<int,int>dis[1500];
for(int i=0;i<stones.size();i++)
po[stones[i]]=i;
dis[0][1]=1;
if(stones[1]!=stones[0]+1)return false;
dis[1][1]=dis[1][2]=1;
for(int i=1;i<stones.size();i++)
{
dis[i-1].clear();
map<int,int>::iterator it;
for(it=dis[i].begin();it!=dis[i].end();it++)
{
if(it->first<=0)continue;
int np=stones[i]+it->first; if(po.count(np))
{
if(it->first>1)dis[po[np]][it->first-1]=1;
dis[po[np]][it->first]=1;
if(it->first<=n)dis[po[np]][it->first+1]=1;
}
}
}
if(dis[n-1].size()>0)return true;
else return false;
}
};
第十七周 Leetcode 403. Frog Jump(HARD) 线性dp的更多相关文章
- [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
https://leetcode.com/contest/5/problems/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
题目如下: 解题思路:我的做法是建立一个字典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 466. Count The Repetitions 倍增DP (HARD)
Leetcode 466 直接给出DP方程 dp[i][k]=dp[i][k-1]+dp[(i+dp[i][k-1])%len1][k-1]; dp[i][k]表示从字符串s1的第i位开始匹配2^k个 ...
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- 201771010134杨其菊《面向对象程序设计(java)》第十七周学习总结
第十七周学习总结 1. 程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程.操作系统为每个进程分配一段独立的内存空间和系统资源,包 ...
随机推荐
- PTA 04-树5 Root of AVL Tree (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree (25分) An AVL tree ...
- [codeforces724D]Dense Subsequence
[codeforces724D]Dense Subsequence 试题描述 You are given a string s, consisting of lowercase English let ...
- Linux Bash对拍
代码: #!/bin/bash while true; do ./rand > input //数据生成器 ./test < input > output //测试程序 ./std ...
- Vim enhance part1
NO1 .认识.命令 例 删除man.config中第1到30行的注释 1.光标移到#上,按下x删除 2.按下j将光标移到第二行#上,之后按下. 3.可以看到第2行的#也被删除了因为.就是重复上次命令 ...
- [NOIP2002] 提高组 洛谷P1031 均分纸牌
题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 ...
- Java线程的5种状态及切换(透彻讲解)
http://blog.csdn.net/pange1991/article/details/53860651
- Help him--hdu5059(模拟 大坑)
http://acm.hdu.edu.cn/showproblem.php?pid=5059 直接说可能出现的情况 #include <iostream> #include <cst ...
- Eclipse移植项目时JDK版本不匹配Project facet Java version 1.7 is not supported
Eclipse移植项目时JDK版本不匹配Project facet Java version 1.7 is not supported 如果原有项目用的为JDK1.7,而自己的是低版本JDK,比如1. ...
- 利用Druid实现应用和SQL监控
一.关于Druid Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource 高效可 ...
- Linux 思维导图
1.Linux学习路径: 2.Linux桌面介绍: 3.FHS(文件系统目录标准): 以上三张图,都是在学习实验楼上的课程--Linux 基础入门,教程里面看到的. 4.Linux需要特别注意的目录: ...