第十七周 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. 程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程.操作系统为每个进程分配一段独立的内存空间和系统资源,包 ...
随机推荐
- 如何在非localhost情况下访问Istio中的服务UI
在使用Istio时经常会遇到需要用localhost访问服务UI才能看到相关的一些数据 但对于远程连接的时候使用localhost并不方便,所以需要修改一下它的部署文件,将原先的cluster IP改 ...
- 在后台根据单据标识构建单据的DynamicObject,然后调用BOS的保存服务保存单据。
var bussnessInfo = Kingdee.BOS.ServiceHelper.MetaDataServiceHelper.GetFormMetaData(this.Context, &qu ...
- Http、TCP/IP、Socket的区别
网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...
- CodeForces 599B Spongebob and Joke
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- uva 1364
刘书上例题 #include <cstdio> #include <cstdlib> #include <cmath> #include <set> # ...
- loj6172 Samjia和大树(树形DP+找规律)
题目: https://loj.ac/problem/6172 分析: 首先容易得出这样的dp式子 然后发现后面那个Σ其实是两段区间,可以用总和减去中间一段区间表示,所以只要维护个前缀和就ok了 这样 ...
- Android 学习路线图(转载自https://blog.csdn.net/lixuce1234/article/details/77947405)
程序设计 一.java (a)基本语法(如继承.异常.引用.泛型等) Java核心技术 卷I(适合入门) 进阶 Effective Java中文版(如何写好的Java代码) Java解惑 (介绍烂Ja ...
- Vue基础学习
使用vue-cli构建初始化vue项目 vue init webpack myfirst 项目截图:(开发工具:webStorm) 主要练习了vue的基本指令:v-bind.v-if.v-show.v ...
- windows 平台使用wireshark命令行抓包
Windows网络流量大,或则需要长时间抓包时,wireshark图形界面使用起来比较麻烦 wireshark 内置 dumpcap命令 Capture interface: -i <inte ...
- 【TFS 2017 CI/CD系列 - 01】-- Agent篇
一.环境要求: PowerShell 3.0 或者更高版本 VS2015或者更高版本 二.下载Agent: 用浏览器打开TFS,[Settings]--> [Agent Pools]--> ...