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;
}
};

  

第十七周 Leetcode 403. Frog Jump(HARD) 线性dp的更多相关文章

  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

    https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...

  3. [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 ...

  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 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个 ...

  8. 【一天一道LeetCode】#55. Jump Game

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

  9. 201771010134杨其菊《面向对象程序设计(java)》第十七周学习总结

    第十七周学习总结 1. 程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程.操作系统为每个进程分配一段独立的内存空间和系统资源,包 ...

随机推荐

  1. [codeforces538E]Demiurges Play Again

    [codeforces538E]Demiurges Play Again 试题描述 Demiurges Shambambukli and Mazukta love to watch the games ...

  2. centos 6.5 yum安装lnmp

    转自:http://blog.csdn.net/lane_l/article/details/20235909 准备篇: 端口 vi /etc/sysconfig/iptables -A INPUT ...

  3. ES6__class 的继承等相关知识案例

    /** * class 的继承等相关知识 */ // extends. static. super const canvas = document.querySelector('#canvas'); ...

  4. msp430项目编程15

    msp430中项目---简易红外遥控系统 1.红外工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习

  5. PHP 基础复习 2018-06-17

    (1)AJAX Asynchronous JavaScript And XML(异步 JavaScript 及 XML) (2)创建 XMLHttpRequest 对象 不同的浏览器使用不同的方法来创 ...

  6. Java操作XML牛逼利器JDOM&DOM4J

    JDOM  JDOM 是一种使用 XML(标准通用标记语言下的一个子集) 的独特 Java 工具包,用于快速开发 XML 应用 程序. JDOM 官方网站:http://www.jdom.org/ 利 ...

  7. Jquery操作事件

    1.文档加载事件 2.DOM单击双击事件 3.DOM获得焦点,失去焦点问题 4.DOM鼠标移入,移出事件 <!DOCTYPE html> <html> <head> ...

  8. try catch finally执行顺序 (return / 变量覆盖)

    finally有return 始终返回finally中的return 抛弃 try 与catch中的return 情况1:try{} catch(){}finally{} return x; try{ ...

  9. leetcode笔记:Longest Substring Without Repeating Characters

    一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...

  10. Reload file in vim

    68down voteaccepted Give this a try: :e From :h :e: Edit the current file. This is useful to re-edit ...