题目链接

Jump Game - LeetCode

注意点

解法

解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能。并且如果i超过了能到达的最大距离说明不能到达,因为i是每次加一都能超过最大距离,小于i的所有位置都会走到某个最远距离为0的位置。时间复杂度O(n)

class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(),maxPos = 0;
for(int i = 0;i <= maxPos;i++)
{
if(maxPos >= n-1) return true;
maxPos = max(maxPos,i+nums[i]);
}
return false;
}
};

解法二:网上看来的解法 —— 动态规划。但是我并不能理解这个状态转移方程dp[i] = max(dp[i - 1], nums[i - 1]) - 1

class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(),pre = nums[0];
for(int i = 1;i < n;i++)
{
pre = max(pre,nums[i-1])-1;
if(pre < 0) return false;
}
return true;
}
};

小结

  • 希望有大神可以详说一下解法二的思路 TAT

Jump Game - LeetCode的更多相关文章

  1. 55. Jump Game leetcode

    55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...

  2. Jump Game —— LeetCode

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. Jump Game leetcode java

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  6. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

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

  8. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. Hyperledger Fabric Capabilities——超级账本功能汇总

    Hyperledger Fabric是一种模块化的区块链架构,是分布式记账技术(DLT)的一种独特的实现,它提供了可供企业运用的网络,具备安全.可伸缩.加密和可执行等特性.Hyperledger Fa ...

  2. QT中的小细节

    一 .  QT4和QT5的区别(信号和槽):1.  QT4: connect(button,SIGNAL(pressed()),this,SLOT(close())); /** * 优点 :写法简单 ...

  3. Hadoop Streaming框架使用(二)

    上一篇文章介绍了Streaming的各种参数,本文具体介绍使用方法. 提交hadoop任务示例: $HADOOP_HOME/bin/hadoop streaming \ -input /user/te ...

  4. Final阶段版本控制报告

    版本控制代码及文档要求 在coding.net版本控制; 公开项目,教师.专家.其他同学可以不注册源代码.在此公布git地址. 报告beta阶段2周中,项目的版本控制情况,不包括未在coding.ne ...

  5. 初步认识CNN

    1.机器学习 (1)监督学习:有数据和标签 (2)非监督学习:只有数据,没有标签 (3)半监督学习:监督学习+非监督学习 (4)强化学习:从经验中总结提升 (5)遗传算法:适者生存,不适者淘汰 2.神 ...

  6. Daily Scrumming* 2015.10.30(Day 11)

    一.总体情况总结 今日项目总结: 1.前后端同一了API设计以及API权限认证.用户状态保存的开发方案 2.API以及后端模型已经开始开发,前端UEditor开始学习,本周任务有良好的起步 3.前后端 ...

  7. 构造一个简单的Linux内核的MenuOS

    构造一个简单的Linux内核的MenuOS 20135109 高艺桐 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000 ...

  8. n以内的1的个数

    import java.util.Scanner; public class main { /** * @param args */ public static void main(String[] ...

  9. Task 5.1 电梯调度程序需求调研报告

    1.任务概述: 1.1任务背景:试想一下,石家庄铁道大学基础教学楼的电梯配置如下:大厦有18层, 4部电梯,很多乘客使用这些电梯的日常(旅客重量:平均70公斤最大120公斤,最小45公斤).其他常量数 ...

  10. 【贪心算法】POJ-1862 简单哈夫曼

    一.题目 Description Our chemical biologists have invented a new very useful form of life called stripie ...