【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 ...
随机推荐
- nvcc 编译显示寄存器使用情况
NVCC Compiler 里面增加 Command line pattern中${COMMAND}后 增加选项: --ptxas-options=-v
- Unity光晕剑效果的Shader简单实现
最近遇到了一个需求,想要一种在刀剑上带有光晕的酷炫效果,甚至是还想要有闪烁呼吸效果,于是就写了一个简单的叫LightSwrod的Shader去实现,先上图看看效果吧. 简单展示 这是剑本身的样子 这是 ...
- python基础-字符串操作
输出高亮 语法: 显示方式.前景色.背景色至少一个存在即可. 显示方式:0(关闭所有效果),1(高亮),4(下划线),5(闪烁),7(反色),8(不可见). 前景色以3开头,背景色以4开头,具体颜 ...
- tcpick
tcpick 是一款基于文本的嗅探器,能追踪,重组和重排tcp流.
- [Git] Create a new repository on the command line
echo "# xxx" >> README.md git init git add README.md git commit -m "first commi ...
- 部署git服务器(Windows Server 2008)
原来的这个项目是一个人开发的,没有做版本管理,我接手后准备搭建git版本管理服务端,方便离线开发和做版本管理: 一台云主机,操作系统:Windows Server 2008,64位: java已经安装 ...
- Java环境变量搭建(Linux环境)
1. 下载解压JDK压缩包 例如:解压到 /opt/jdk1.7.0_80 下 2. 添加环境变量到 /etc/profile 文件中 vi /etc/profile 在文件末尾追加如下内容: exp ...
- 解决Win10桌面右键卡顿一直转圈圈的
把系统重置之后,发现在桌面点击右键时一直转圈,但是在文件夹等非桌面位置都正常.可能是我之前修改注册表添加右键选项造成的,也可能不是,因为将修改的地方删除还是没有解决问题,555. 上网搜素一波,发现大 ...
- 使用vue-cli创建项目
使用Vue UI创建.管理项目 1.全局安装vue-cli 3.0 npm install -g @vue/cli 2.启动vue ui 创建项目: vue ui
- SQL Server中通用数据库角色权限的处理详解
SQL Server中通用数据库角色权限的处理详解 前言 安全性是所有数据库管理系统的一个重要特征.理解安全性问题是理解数据库管理系统安全性机制的前提. 最近和同事在做数据库权限清理的事情,主要是删除 ...