【Jump Game】cpp
题目:
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
.
代码:
class Solution {
public:
bool canJump(vector<int>& nums)
{
return Solution::dfs(nums, );
}
static bool dfs(vector<int>& nums, int index)
{
if ( index>=nums.size()- ) return true;
if ( nums[index]== ) return false;
for ( int i = nums[index] ; i >= ; --i )
{
if ( Solution::dfs(nums, index+i) ) return true;
}
return false;
}
};
tips:
随手写了一个DFS Solution,结果是对的但是超时,shit...
====================================
由于不会Greedy的算法,一心想写一个dp solution,结果最后dp没写成,写成了个greedy;不过代码还是很简洁和高效的:
class Solution {
public:
bool canJump(vector<int>& nums)
{
int max_jump = ;
max_jump = std::max(max_jump, nums[]);
for ( int i = ; i<=max_jump; ++i )
{
if ( max_jump>=nums.size()- ) return true;
max_jump = std::max(max_jump, i+nums[i]);
}
return false;
}
};
tips:
算法的时间复杂度O(n),空间复杂度O(1)。
正常往后迭代变量,每次迭代变量后,维护一个max_jump(即走到元素i,已知可以走到最远的元素下标)。
如果下标大于等于nums.size()-1,则返回true;如果遍历到max_jump了,且没有到达nums.size()-1,则返回false。
后来想想能不能用dp或者dfs再解一次,但想来想去,dp或者dfs解法的核心无非还是变形的greedy,没有太大意思。完毕。
===============================================
第二次过这道题,直接写了greedy的AC了。
class Solution {
public:
bool canJump(vector<int>& nums) {
if ( nums.size()== ) return false;
int maxLength = ;
for ( int i=; i<=maxLength; ++i )
{
if ( i+nums[i]>maxLength )
{
maxLength = i+nums[i];
}
if ( maxLength>=nums.size()- ) return true;
}
return maxLength>=nums.size()-;
}
};
【Jump Game】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
随机推荐
- uvm_port_base——TLM1事务级建模方法(五)
文件: src/tlm1/uvm_port_base.svh 类: uvm_port_base uvm_port_component_base派生自uvm_component,因此具有其所有特性.提供 ...
- Linux文件的三个时间属性(Atime,Mtime,Ctime)
Linux下,一个文件有三种时间,分别是: 访问时间:atime 修改时间:mtime 状态时间:ctime 访问时间:对文件进行一次读操作,它的访问时间就会改变.例如像:cat.more等操作,但是 ...
- 在SQL中查看文件组中有哪些表
SELECT o.[name], o.[type], i.[name], i.[index_id], f.[name] FROM sys.indexes i INNER JOIN sys.filegr ...
- php使用GD库实现图片水印和缩略图——给图片添加文字水印
今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...
- 基于PowerShell的Lync Server管理 使用C# 之 Telephony 功能 查看 /修改
本以为这个属性可以在用户信息中直接反应出来,但是看了好几遍还是没找到这个属性名称 这个功能没有在get-User 的结果中直接反映出来 但是可以通过 Property 查找单个选项 如: Get-Cs ...
- Sql Server 表的复制
声名:A,B ,都是表 --B表存在(两表结构一样)insert into B select * from A 若两表只是有部分(字段)相同,则 insert into B(col1,col2,col ...
- JS中的toString()和valueOf()方法
1.toString()方法:主要用于Array.Boolean.Date.Error.Function.Number等对象转化为字符串形式.日期类的toString()方法返回一个可读的日期和字符串 ...
- PHP读取文件的常见方法
整理了一下PHP中读取文件的几个方法,方便以后查阅. 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件 ...
- spring-boot自定义启动端口
有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...
- C# File和Directory类
File和Directory类 作为实用类,File和Directory类都提供了许多方法,用于处理文件系统以及其中的文件和目录.这些是静态方法,涉及移动文件.查询和更新属性并创建FileStream ...