leetcode || 45、 Jump Game II

problem:

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.)

Hide Tags

Array Greedy

题意:站在一个数组的起点,最远可以跳跃位置处数组值的距离,求到达数组末尾的最小的跳跃次数。

thinking:

(1)题意较简单,很容易建立模型:贪心策略选择下一次的跳跃位置,使得这一次跳跃的成果最大化!!!

(2)怎么样衡量选择的成果呢? 有两个因素:下一次跳跃位置的数值 和 下一次跳跃的位置

code:

class Solution {
public:
    int jump(int A[], int n) {
        int step=0;//步数
        int loc=0;//位置
        if(n==1) //不用再跳了
            return 0;
        while(loc<n)
        {
           if(A[loc]+loc>=n-1) //最后一跳
               return (step+1);
           int flag=1;
           int max=A[loc+1]+1;  //初始化 成果衡量 参数
           for(int i=1;i<=A[loc];i++)
           {
               if(A[loc+i]+i>max)  //取得成果最大化
               {
                   max=A[loc+i]+i;
                   flag=i;
               }
           }//for
           loc+=flag;  //完成一次跳跃
           step++;
        }
        return step;

    }
};
时间: 2024-07-31 19:57:30

leetcode || 45、 Jump Game II的相关文章

leetcode笔记:Jump Game II

一. 题目描述 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

LeetCode OJ:Jump Game II(跳跃游戏2)

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

leetcode || 55、Jump Game

problem: 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 exa

leetcode || 63、Unique Paths II

problem: 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

leetcode || 140、Word Break II

problem: 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, given s = "catsanddog", dict = ["cat", "

leetcode || 137、Single Number II

problem: 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? Hide Tags Bit Manipulation 题

leetcode || 132、Palindrome Partitioning II

problem: 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 ["

BFS问题-LeetCode 55、45、5297、127、433、434(BFS)

[LeetCode #55]跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置. class Solution { public: bool canJump(vector& nums) { int maxReach = nums[0];

leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

Jump Game II 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 nu