第十七周 Leetcode 403. Frog Jump(HARD) 线性dp

leetcode403

我们维护青蛙从某个石头上可以跳那些长度的距离即可 用平衡树维护。

总的复杂度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;
    }
};

  

时间: 2024-08-04 10:08:53

第十七周 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 exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

[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 exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

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 exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

403 Frog Jump 青蛙过河

一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一个石子上). 开始时, 青蛙默认已站在第一个石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格1跳至单元格2).如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1.k 或 k + 1个单位. 另请注意,青蛙只能向前方(终

第七周 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的第k位开始匹配2^k个s2串需要的长度 最后通过一个循环 累积最多可以匹配多少个s2串 除以n2下取整就是答案 用倍增加速后 总的复杂度nlogn 而本题的n非常小 轻松AC 体会到倍增的魅力了吧. const int maxn=100+1,INF=1e+9; long long int dp[maxn][30]; cl

软件工程第十七周学习进度条

时间 学习花费时间 (小时) 代码量(行) 博客量(篇) 了解到的知识点 不足 第十七周 周一 (8.00-9.50)上课 周三.周四下午:继续团队项目,完成团队项目的相关作业 215 4    

第十七周

第十七周     日期  星期一   星期二   星期三   星期四   星期五   星期六   星期日  了解到的知识点     jqurey实现下拉菜单改变事件 $(document).ready(function(){    $(".gaibian").change(function(){ var juese1 = $(this).val();//获取下拉单的值 } 文本域如果是英文字母的话不会换行 用如下解决 <td style="WORD-break: bre

第十七周进度报告

第十七周 所花时间(包括上课) 18h(上课4h,阅读2h编程12h) 代码量(行) 大约900h 博客量(篇) 2篇 了解到的知识点 开发团队软件

第十七周学习进度

  第十七周 代码量 2000 所花时间 15 博客量 4 学到的知识点 Framework – 解决问题的框架,怎么开好一个 Postmortem 会议,安卓程序的总结,优化 总结分析: 这是最后一周了,头一次和大二的拉票,拼着嗓子介绍自己的产品,为了争取一票又送冰棍又送糖,最后汗流满面,通过这次经历,感觉很奇特,回味无穷.