力扣第三十一天(DP topic)

This blog covers two problems from LeetCode - Jump Game I and Jump Game II. It discusses how to solve these challenges using dynamic programming techniques, providing time complexity and space complexity analysis for each solution. The focus is on understanding the greedy approach to minimize jumps in reaching the end of the array." 117616644,10545505,Android TextView属性详解:文本显示与布局控制,"['Android开发', 'UI设计', '文本显示', '控件属性']
摘要由CSDN通过智能技术生成

problem Ⅰ

55. Jump Game
You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

solution 1

class Solution {
public:
    bool canJump(vector<int>& nums) {
        if(nums.size()==1)return true;
        if(!nums[0])return false;
        for(int i=1; i<nums.size()-1; i++){
            nums[i] = max(nums[i], nums[i-1]-1);
            if(!nums[i])return false;
        }
        return nums[nums.size()-2];
    }
};

time complexity : O ( n ) O(n) O(n)
space complexity : O ( 1 ) O(1) O(1)

solution 2

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int i = 0;
        for (int reach = 0; i < nums.size() && i <= reach; ++i)
            reach = max(i + nums[i], reach);
        return i == nums.size();
    }
};

在这里插入图片描述

problem Ⅱ

45. Jump Game II
Given an array of non-negative integers nums, 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.

You can assume that you can always reach the last index.

Example 1:

Input: nums = [2,3,1,1,4]
Output: 2
Explanation: 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.

Example 2:

Input: nums = [2,3,0,1,4]
Output: 2

solution

class Solution {
public:
    int jump(vector<int>& nums) {
        int currBegin=0, currEnd=0, currFar=0, jumps=0;
        for(int i=0; i<nums.size()-1; i++){
            currFar = max(currFar, i + nums[i]);
            if(i==currEnd){
                ++jumps;
                currEnd = currFar;
            }
        }
        return jumps;
    }
};

time complexity : O ( n ) O(n) O(n)
space complexity : O ( 1 ) O(1) O(1)
在这里插入图片描述
NOTE:
the main idea is based on greedy, at first the range of the current jump is [currBegin, currEnd], currFar is the farthest we can jump by the element in the current range.

once the i meet the currEnd, which means that we have iterate all element in the current range, and we get the currFar that we can jump from the begin in the current range, and we set the new currEnd equals to currFar, then keep the above steps, finally we can get the ans.
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值