算法训练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

相关推荐
小O的算法实验室7 分钟前
2026年IEEE TITS,面向按需外卖配送调度的特定问题知识与基于学习元启发式算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
加勒比海带669 分钟前
目标检测算法——农林行业数据集汇总附下载链接【Plant】
大数据·图像处理·人工智能·算法·目标检测
洛水水10 分钟前
【力扣100题】23. 螺旋矩阵
算法·leetcode·矩阵
影sir36 分钟前
不同测试数据下,该如何选择算法
算法·深度优先
潇湘散客1 小时前
CAX软件插件化设计实现牛刀小试
c++·算法·图形学·opengl
速易达网络1 小时前
2026,视觉算法正在经历一场静默革命
算法
WBluuue1 小时前
Codeforces 1094 Div1+2(ABCDE)
c++·算法
TENSORTEC腾视科技2 小时前
腾视科技大模型一体机解决方案:低成本私有化落地,重塑行业智能应用新格局
大数据·人工智能·科技·算法·ai·零售·大模型一体机
酿情师2 小时前
区块链原理与技术02:区块链的数据结构04(区块结构)
数据结构·区块链
夏日听雨眠2 小时前
数据结构(循环队列)
数据结构·算法·链表