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

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

相关推荐
夏鹏今天学习了吗2 小时前
【LeetCode热题100(79/100)】打家劫舍
算法·leetcode·职场和发展
iAkuya2 小时前
(leetcode)力扣100 41二叉树的层序遍历(bfs)
windows·leetcode·宽度优先
老鼠只爱大米2 小时前
LeetCode算法题详解 53:最大子数组和
leetcode·动态规划·分治法·最大子数组和·kadane算法·maxsubarray
小欣加油2 小时前
leetcode 面试题17.16 按摩师
数据结构·c++·算法·leetcode·动态规划
飞Link2 小时前
数据合成中的通用模型蒸馏、领域模型蒸馏和模型自我提升
算法·数据挖掘
夏鹏今天学习了吗2 小时前
【LeetCode热题100(80/100)】完全平方数
算法·leetcode·职场和发展
sin_hielo2 小时前
leetcode 3454(扫描线模板题:矩形面积并)
数据结构·算法·leetcode
地球资源数据云2 小时前
1960年-2024年中国农村居民消费价格指数数据集
大数据·数据库·人工智能·算法·数据集
爱编程的小吴3 小时前
【力扣练习题】167. 两数之和 II - 输入有序数组
算法·leetcode·职场和发展