【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=0, next_max_jump=0, min_step=0;
            for ( int i = 0; i<=max_jump; ++i )
            {
                if ( max_jump>=nums.size()-1 ) 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

完毕

时间: 2024-10-08 00:01:06

【Jump Game II 】cpp的相关文章

【Unique Paths II】cpp

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the m

【Word Break II】cpp

题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats&q

【Path Sum II】cpp

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 代码: /** * Definition f

【Spiral Matrix II】cpp

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 代码: class Solution { public: vector<vector<

【palindrome partitioning II】cpp

题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa&qu

【Single Num II】cpp

题目: Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 代码: class Solution { public: int s

【Pascal&#39;s Triangle II 】cpp

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码: class Solution { public: vector<int> getRow(int rowIndex) { vector

【Pascal&#39;s Triangle】cpp

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码: class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector

【大话存储II】学习笔记(15章),文件级集群系统

[大话存储II]学习笔记(15章),块级集群存储系统里面分析的主要是块集群系统,同样文件级存储也可以集群化. 因为NAS系统的前端网络是以太网,速度比较低,导致NAS主要用于一些非关键业务中,比如文件共享.但是一些特殊应用也需要多主机同时访问某个大文件,比如3D渲染集群等.如果我们使用块集群存储系统,则会存在一个问题,需要在应用程序上引入文件锁,而NAS的文件系统一般都自带有文件锁机制,所以还不如把NAS存储系统进行集群化. 在谈怎么样把NAS系统进行集群化之前,我们说说集群文件系统的架构.集群