【Jump Game II 】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.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2
. (Jump 1
step from index 0 to 1, then 3
steps to the last index.)
代码:
class Solution {
public:
int jump(vector<int>& nums) {
int max_jump=, next_max_jump=, min_step=;
for ( int i = ; i<=max_jump; ++i )
{
if ( max_jump>=nums.size()- ) break;
if ( max_jump < i+nums[i] ) next_max_jump = std::max(next_max_jump, i+nums[i]);
if ( i==max_jump )
{
max_jump = next_max_jump;
min_step++;
}
}
return min_step;
}
};
tips:
参考Jump Game的Greedy思路。
这道题要求求出所有可能到达路径中的最短步长,为了保持O(n)的解法继续用Greedy。
这道题的核心在于贪心维护两个变量:
max_jump:记录上一次跳跃能跳到最大的下标位置
next_max_jump:记录遍历所有max_jump之前的元素后,下一次可能跳到的最大下标位置
举例说明如下:
原始数组如右边所示:{7,0,9,6,9,6,1,7,9,0,1,2,9,0,3}
初始:max_jump=0 next_max_jump=0 min_step=1
i=0:next_max_jump=7 更新max_jump=7 更新min_step=1
i=1: 各个值不变
i=2: i+nums[i]=2+9=11>7 更新next_max_jump=11
i=3:i+nums[i]=3+6=9<11 不做更新
i=4: i+nums[i]=4+9=13>11 更新max_jump=13
...
i=7:i+nums[i]=7+7=14 >= nums.size()-1 返回min_step=2
完毕
==========================================
第二次过这道题,不太顺,大体复习了下思路。
class Solution {
public:
int jump(vector<int>& nums) {
if ( nums.size()== ) return ;
int ret = ;
int nextJump = ;
int maxLength = ;
for ( int i=; i<=maxLength; ++i )
{
if ( maxLength>=nums.size()- ) break;
nextJump = std::max(i+nums[i], nextJump);
if ( i==maxLength )
{
maxLength = nextJump;
ret++;
}
}
return ret;
}
};
==========================================
第三次过这道题,把代码改了一行,但是整体结构清晰了不少。
class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size()<) return ;
int steps = ;
int local = ;
int next = local;
for ( int i=; i<=local; ++i )
{
next = max(next, i+nums[i]);
if ( i==local )
{
steps++;
local = next;
if ( local>=nums.size()- ) return steps;
}
}
return ;
}
};
next只负责看下一跳能够到哪。
什么时候更新local了,再判断能否跳到尾部。
【Jump Game II 】cpp的更多相关文章
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【Path Sum II】cpp
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- 【N-Quens II】cpp
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
随机推荐
- Android 浮动按钮的伸缩效果
在做项目时想增加点动感,于是就有如下效果: 实现起来也很简单,通过属性动画和recyclerview 滑动结合就很好实现了. 通过给recycleview添加一个滑动监听:通过滚动的差值来处理动画 m ...
- Eucalyptus管理页面密码设置
桉树环境什么的都已经是配置好了的,但是过了一段时间不用,也不知道密码是什么了,看着下面的页面也不知道如何进去,这里我们通过命令行的方式重置用户名和密码信息. 登陆clc所在机器,输入下命令: euar ...
- SQL Server(第二章) 字符串函数、日期时间函数、转换函数
--1.CONCAT 函数:字符串连接(支持sql server2012 SQL规则 如果与NULL连接返回NILL) SELECT empid,CONCAT(firstname,lastname) ...
- sql server 2012安装程序图
重点:下面的安装步骤都在断网环境下安装的 因为我想查看联网跟没有联网SQL2012会不会下载并安装.net2.0 和.net3.5和.net4和SP1补丁包 我的环境: 没有集成SP1补丁包的安装包大 ...
- cms-后台eazyui搭建
1.引入eazyUi需要的js 2.布局:上.下.左.中 <%@ page language="java" contentType="text/html; char ...
- Nginx源码安装及调优配置(转)
导读 由于Nginx本身的一些优点,轻量,开源,易用,越来越多的公司使用nginx作为自己公司的web应用服务器,本文详细介绍nginx源码安装的同时并对nginx进行优化配置. Nginx编译前 ...
- Bootstrap 弹出框(Popover)插件
Bootstrap 弹出框(Popover)插件与Bootstrap 提示工具(Tooltip)插件类似,提供了一个扩展的视图,用户只需要把鼠标指针悬停到元素上面即可.弹出框的内容完全由Bootstr ...
- React后台管理系统-商品管理列表组件
1.商品列表页面结构 <div id="page-wrapper"> <PageTitle title="商品列表" ...
- 项目:Vue+node+后台管理项目小结
序:本文主要分两块说:项目机制,具体用到的知识块. 1. 项目机制 项目的原型以vue-cli为原型,进行项目的初步构建.项目以node.js服务和webpack打包机制为依托,将.vue文件打包为浏 ...
- java算法面试题:排序都有哪几种方法?请列举。用JAVA实现一个快速排序。选择冒泡快速集合至少4种方法排序
package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...