【leetcode】 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
题意:给定一组数组,每个元素代表在此位置能够跳跃的最大距离,判断是否能够跳到最后一个下标。
有三种思路:
- 正向从 0 出发,一层一层往右跳,看最后能不能超过最右下标,能超过,说明能到达,否则不能到达。
- 逆向出发,一层一层递减,看能不能到达0.
- 可以用动规,设状态为 f[i],表示从第 0 层出发,走到 A[i] 时剩余的最大步数,则状态转移方程为:f [i] = max(f [i − 1], A[i − 1]) − 1, i > 0
#include <iostream>
#include <vector> using namespace std; class Solution {
public:
bool canJump(int A[], int n) {
int reach = ;//the right most position can reach
for(int i = ; i < reach && reach < n; i++)
reach = max(reach, i + + A[i]);
return reach >= n;
} bool canJump1(int A[], int n) {
if(n == ) return true;
int left_most = n - ;//the left most position can reach
for(int i = n - ; i >= ; --i)
if(i + A[i] >= left_most)
left_most = i;
return left_most == ;
} bool canJumpDp(int A[],int n){
//f [i] = max(f [i − 1], A[i − 1]) − 1, i > 0
vector<int> f(n, );//hold the state
f[] = ;
for(int i = ; i < n; ++i){
f[i] = max(f[ i - ], A[i - ]) - ;
if(f[i] < )
return false;
}
return f[n - ] >= ;
}
}; int main()
{
Solution s;
int A1[] = {,,,,};
int A2[] = {,,,,};
cout << s.canJumpDp(A1, ) << endl;
cout << s.canJumpDp(A2, ) << endl;
return ;
}
【leetcode】 Jump Game的更多相关文章
- 【LeetCode】Jump Game (一维动态规划 + 线性扫描)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode】Jump Game I & II (hard)
Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...
- 【Leetcode】Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode】Jump Game I, II 跳跃游戏一和二
题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...
- 【LeetCode】Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- [Deep-Learning-with-Python] Keras高级概念
Keras API 目前为止,介绍的神经网络模型都是通过Sequential模型来实现的.Sequential模型假设神经网络模型只有一个输入一个输出,而且模型的网络层是线性堆叠在一起的. 这是一个经 ...
- Ubuntu中程序部署时无法加载动态库的解决方法
Ubuntu下修改环境变量的三种方法 添加环境变量无法解决,可尝试如下操作: sudo vim /etc/ld.so.conf 在ld.so.conf中加入动态库的目录... 然后 sudo ldco ...
- CS50.3
1,int()取整函数 2,RPG(role playing game )角色扮演游戏 3,代码写了,要跑,需要compiler (编译器) 4,CLI(command-line interface) ...
- Redis基本数据类型介绍笔记
Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...
- Jmeter(二十二)_jenkins配置gitlab插件与ant插件
Docker部署接口自动化持续集成环境第四步,代码上传到远程仓库! 接上文:脚本上传Gitlab 服务器中的Jenkins通过Gitlab插件读取远程Git远程仓库中的代码,然后通过ant插件进行构建 ...
- 通用shellcode
所有 win_32 程序都会加载 ntdll.dll 和 kernel32.dll 这两个最基础的动态链接库.如果想要 在 win_32 平台下定位 kernel32.dll 中的 API 地址,可以 ...
- Muduo学习笔记(一) 什么都不做的EventLoop
Muduo学习笔记(一) 什么都不做的EventLoop EventLoop EventLoop的基本接口包括构造.析构.loop(). One Loop Per Thread 一个线程只有一个Eve ...
- 分分钟让你理解HTTPS
一.HTTP存在的问题 1.1 可能被窃听 HTTP 本身不具备加密的功能,HTTP 报文使用明文方式发送 由于互联网是由联通世界各个地方的网络设施组成,所有发送和接收经过某些设备的数据都可能被截获或 ...
- Linux shell(1)
Linux的Shell种类众多,常见的有:Bourne Shell(/usr/bin/sh或/bin/sh).Bourne Again Shell(/bin/bash).C Shell(/usr/bi ...
- 软工实践练习一 git使用心得
使用git进行代码管理的心得 小组 1.结对的同学创建了小组,我属于被邀请的.附上图片一张. 2.已将代码库https://github.com/sefzu2015/AutoCS fork到了小组or ...