Day41 >> 121、买卖股票的最佳时机 + 122.买卖股票的最佳时机II + 123.买卖股票的最佳时机III

代码随想录-动态规划Part8

121、买卖股票的最佳时机

java 复制代码
class Solution {
    public int maxProfit(int[] prices) {
        int[] dp = new int[2];
        dp[0] = -prices[0];
        dp[1] = 0;
        for (int i = 1; i <= prices.length; i++) {
            dp[0] = Math.max(dp[0], -prices[i - 1]);
            dp[1] = Math.max(dp[1], dp[0] + prices[i - 1]);
        }
        return dp[1];
    }
}

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

java 复制代码
class Solution {
    public int maxProfit(int[] prices) {
        int result = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            result += Math.max(prices[i + 1] - prices[i], 0);
        }
        return result;
    }
}

123.买卖股票的最佳时机III

太累了,,,有种力不从心的感觉......

相关推荐
AIpanda8884 小时前
如何借助AI销冠系统提升数字员工在销售中的成效?
算法
啊阿狸不会拉杆4 小时前
《机器学习导论》第 7 章-聚类
数据结构·人工智能·python·算法·机器学习·数据挖掘·聚类
木非哲4 小时前
机器学习--从“三个臭皮匠”到 XGBoost:揭秘 Boosting 算法的“填坑”艺术
算法·机器学习·boosting
小辉同志4 小时前
437. 路径总和 III
算法·深度优先·广度优先
笨笨阿库娅4 小时前
从零开始的算法基础学习
学习·算法
不想睡觉_5 小时前
优先队列priority_queue
c++·算法
那个村的李富贵13 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
power 雀儿13 小时前
Scaled Dot-Product Attention 分数计算 C++
算法
琹箐14 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
renhongxia114 小时前
如何基于知识图谱进行故障原因、事故原因推理,需要用到哪些算法
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·知识图谱