牛客题解 | 跳跃游戏(一)

题目## 题目

题目链接

解题思路

这是一个判断能否跳到数组末尾的问题,可以通过贪心算法来解决:

  1. 维护一个变量 m a x R e a c h maxReach maxReach,表示当前能够到达的最远位置
  2. 遍历数组,对于每个位置 i i i
    • 如果 i i i 超过了 m a x R e a c h maxReach maxReach,说明无法到达该位置,返回 f a l s e false false
    • 更新 m a x R e a c h = m a x ( m a x R e a c h , i + n u m s [ i ] ) maxReach = max(maxReach, i + nums[i]) maxReach=max(maxReach,i+nums[i])
  3. 如果能遍历完整个数组,说明可以到达末尾

代码

#include <iostream>
#include <vector>
using namespace std;

bool canJump(vector<int>& nums) {
    int n = nums.size();
    int maxReach = 0;  // 当前能到达的最远位置
    
    for (int i = 0; i <= maxReach; i++) {
        // 如果已经可以到达最后一个位置
        if (maxReach >= n - 1) return true;
        
        // 更新最远可达位置
        maxReach = max(maxReach, i + nums[i]);
    }
    
    return false;
}

int main() {
    int n;
    cin >> n;
    vector<int> nums(n);
    for (int i = 0; i < n; i++) {
        cin >> nums[i];
    }
    
    cout << (canJump(nums) ? "true" : "false") << endl;
    return 0;
}
import java.util.*;

public class Main {
    public static boolean canJump(int[] nums) {
        int n = nums.length;
        int maxReach = 0;
        
        for (int i = 0; i <= maxReach; i++) {
            if (maxReach >= n - 1) return true;
            maxReach = Math.max(maxReach, i + nums[i]);
        }
        
        return false;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }
        
        System.out.println(canJump(nums) ? "true" : "false");
    }
}
def canJump(nums):
    n = len(nums)
    max_reach = 0  # 当前能到达的最远位置
    
    for i in range(n):
        # 如果当前位置已经超过了能到达的最远位置
        if i > max_reach:
            return False
            
        # 更新最远可达位置
        max_reach = max(max_reach, i + nums[i])
        
        # 如果已经可以到达最后一个位置
        if max_reach >= n - 1:
            return True
            
    return True

n = int(input())
nums = list(map(int, input().split()))
print("true" if canJump(nums) else "false")

算法及复杂度

  • 算法:贪心算法
  • 时间复杂度: O ( n ) \mathcal{O}(n) O(n) - 只需要遍历一次数组
  • 空间复杂度: O ( 1 ) \mathcal{O}(1) O(1) - 只需要常数额外空间

这道题的关键在于理解贪心的思想:我们只需要关心在当前位置能跳到的最远距离。对于每个位置,我们都更新能到达的最远位置,如果在遍历过程中发现当前位置已经超过了能到达的最远位置,就说明无法到达终点。

几个重要的点:

  1. m a x R e a c h maxReach maxReach 表示当前能到达的最远位置,初始为 0 0 0
  2. 遍历时需要判断当前位置 i i i 是否可达( i ≤ m a x R e a c h i \leq maxReach imaxReach
  3. 对于每个可达位置,更新 m a x R e a c h = m a x ( m a x R e a c h , i + n u m s [ i ] ) maxReach = max(maxReach, i + nums[i]) maxReach=max(maxReach,i+nums[i])
  4. 如果在遍历过程中 m a x R e a c h ≥ n − 1 maxReach \geq n-1 maxReachn1,说明可以到达终点

这种贪心的方法比动态规划更高效,因为我们只需要遍历一次数组,并且只需要维护一个变量。

题目链接

解题思路

这是一道动态规划的题目,我们可以这样分析:

  1. 当青蛙处在第 n n n 级台阶时,它可以从前面的任意一级台阶跳过来
  2. 因此 f ( n ) = f ( n − 1 ) + f ( n − 2 ) + . . . + f ( 1 ) + f ( 0 ) f(n) = f(n-1) + f(n-2) + ... + f(1) + f(0) f(n)=f(n1)+f(n2)+...+f(1)+f(0)
  3. 同理 f ( n − 1 ) = f ( n − 2 ) + f ( n − 3 ) + . . . + f ( 1 ) + f ( 0 ) f(n-1) = f(n-2) + f(n-3) + ... + f(1) + f(0) f(n1)=f(n2)+f(n3)+...+f(1)+f(0)
  4. 两式相减得: f ( n ) = 2 ∗ f ( n − 1 ) f(n) = 2 * f(n-1) f(n)=2f(n1)
  5. 最终可以推导出 f ( n ) = 2 ( n − 1 ) f(n) = 2^(n-1) f(n)=2(n1)

代码

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    cout << (1 << (n-1)) << endl;
    return 0;
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(1 << (n-1));
        sc.close();
    }
}
n = int(input())
print(1 << (n-1))

算法及复杂度

  • 算法:数学推导/动态规划
  • 时间复杂度: O ( 1 ) \mathcal{O}(1) O(1)
  • 空间复杂度: O ( 1 ) \mathcal{O}(1) O(1)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值