算法训练Day28 | ● 122.买卖股票的最佳时机II ● 55. 跳跃游戏 ● 45.跳跃游戏II

122.买卖股票的最佳时机II

cpp 复制代码
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        vector<int> dp(2,0);
        dp[0] = -prices[0];
        for(int i=1; i<prices.size(); i++){
            dp[0] = max(dp[0], dp[1]-prices[i]);
            dp[1] = max(dp[1], prices[i]+dp[0]);
        }
        return dp[1];
    }
};

参考文章:代码随想录- 122.买卖股票的最佳时机II

55. 跳跃游戏

cpp 复制代码
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int cover = 0;
        for(int i=0; i<=cover; i++){
            
            if(i+nums[i]>cover){
                cover = i+nums[i];
            }
            if(cover>=nums.size()-1) return true;
        }
        return false;

    }
};

参考文章:代码随想录-55. 跳跃游戏

45.跳跃游戏II

cpp 复制代码
class Solution {
public:
    int jump(vector<int>& nums) {
        if (nums.size() == 1) return 0;
        int cover = 0;
        int next_cover = 0;
        int count = 0;
        for(int i=0; i<=nums.size(); i++){
            
            if(i+nums[i]>next_cover){
                next_cover = i+nums[i];
            }
            if(i == cover){
                count++;
                cover = next_cover;
                if(next_cover>=nums.size()-1){
                    return count;
                }
                
                
                
            }
        }
        return -1;

    }
};

参考文章:代码随想录- 45.跳跃游戏II

相关推荐
NAGNIP4 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队5 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja10 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下10 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶10 小时前
算法 --- 字符串
算法
博笙困了10 小时前
AcWing学习——差分
c++·算法
NAGNIP10 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP10 小时前
大模型微调框架之LLaMA Factory
算法
echoarts10 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客10 小时前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法