【算法刷题】leetcode jump game

跳跃游戏算法解析
本文探讨了两种不同难度级别的跳跃游戏算法问题。基础版问题旨在判断能否从数组起始位置达到末尾,通过贪心算法实现;进阶版则关注如何以最少步骤到达终点,提供了动态规划及优化后的贪心算法解决方案。

基础版:是否能到达

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], returntrue.

A =[3,2,1,0,4], returnfalse.


利用贪心

    bool canJump(int A[], int n) {
        if(A==nullptr)
            return false;
        int maxnum = A[0];
        for(int i=1;i<n;++i)
        {
            if(i<=maxnum)
            {
                int n=i+A[i];
                maxnum = max(maxnum,n);
            }
        }
        if(maxnum <(n-1))
            return false;
        
        return true;
    }

进阶版    到达最后一个最少的步数

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 is2. (Jump1step from index 0 to 1, then3steps to the last index.)


首先  利用了动态规划,dp[i]表示到达当前位置最小的步数,代码如下

    int jump(int A[], int n) {
        if(A==nullptr)
            return 0;
        int* dp = new int[n];
        dp[0]=0;
        for(int i=1;i<n;++i)
        {
            dp[i] = n+1;
            for(int j=0;j<i;++j)
            {
                if(i<=j+A[j])
                {
                    int temp = dp[j]+1;
                    dp[i] = min(dp[i],temp);
                }
            }
            
        }
        int res = dp[n-1];
        delete []dp;
        return res;
    }

在牛客网上没有提示超时,过了,但是在网上看到说在leetcode上利用动态规划会超时,最好的方法还是利用贪心:


cur是当前能到达的最远位置,即最远能覆盖的位置,last是上一步能到达的最远位置,遍历数组,用i + nums[i]更新cur,然后判断如果当前位置超过了last,即上一步能到达的最远位置,说明需要再跳一次了,并更新last

    int jump(int A[], int n) {
        if(A==nullptr)
            return 0;
        int res(0),last(0),cur(0);
        for(int i=0;i<n;++i)
        {
            
            if(i>last)
            {
                ++res;
                last = cur;
            }
            cur=max(cur,i+A[i]);
        }
        
        
        return res;
    }
### Java 算法练习 LeetCode 目及答案解析 #### 跳跃游戏 (Jump Game) 在跳跃游戏中,给定一个非负整数数组 `nums` ,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断是否能够到达最后一个位置。 ```java class Solution { public boolean canJump(int[] nums) { int max = 0; // 能跳的最远距离 for (int i = 0; i < nums.length; i++) { if (i > max) return false; max = Math.max(max, nums[i] + i); } return true; } } ``` 此代码通过遍历数组来计算能跳到的最远距离,并判断当前位置是否超过了最大可达范围[^1]。 #### 合并区间 (Merge Intervals) 对于合并区间的目,给出一系列闭合区间 `[start,end]` 的列表,需要找到所有重叠区域并将它们合并成一个新的区间列表。 虽然未提供具体实现代码,这类问通常涉及先按起始时间排序再逐一遍历比较相邻区间的方法。 #### 最小栈 (Min Stack) 设计一个支持 push、pop 和 top 操作,并能在常数时间内检索到最小元素的数据结构。 ```java import java.util.Stack; class MinStack { private final Stack<Integer> stack = new Stack<>(); private final Stack<Integer> minStack = new Stack<>(); public void push(int val) { stack.push(val); if (minStack.isEmpty() || val <= getMin()) { minStack.push(val); } } public void pop() { if (!stack.isEmpty() && stack.peek().equals(getMin())) { minStack.pop(); } stack.pop(); } public int top() { return stack.peek(); } public int getMin() { return minStack.peek(); } } ``` 这段代码展示了如何利用两个栈分别保存全部数据以及当前状态下的最小值,在执行基本操作的同时维护好辅助栈的状态以便快速获取最小值[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值