所有股票问题基本都可以使用动态规划进行求解。
本文所使用的动态规划方法均参考一下链接:
买卖股票专题讲解

买卖股票之只能一次交易

# # 一次遍历
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         max_p = 0
#         min_p = prices[0]
#         for i in range(1, len(prices)):
#             min_p = min(min_p, prices[i])
#             max_p = max(prices[i] - min_p, max_p)
#         return max_p# #一维动态规划
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         dp = [0 for _ in range(len(prices))]
#         min_p = prices[0]
#         for i in range(1, len(prices)):
#             min_p = min(min_p, prices[i])
#             dp[i] = max(dp[i - 1], prices[i] - min_p)
#         return dp[-1]# #二维动态规划
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         dp = [[0] * 2 for _ in range(len(prices))]
#         dp[0][0] = -prices[0]
#         for i in range(1, len(prices)):
#             dp[i][0] = max(dp[i - 1][0], - prices[i])
#             dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])
#         return dp[-1][1]# 单调栈
class Solution:def maxProfit(self, prices: List[int]) -> int:stack = [prices[0]]ans = 0for i in range(1, len(prices)):if prices[i] > stack[-1]:ans = max(prices[i] - stack[-1], ans)else:stack.append(prices[i])return ans

买卖股票之可多次交易

# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         stack = [prices[0]]
#         ans = 0
#         for i in range(1, len(prices)):
#             if stack[-1] <= prices[i]:
#                 stack.append(prices[i])
#             else:
#                 ans += stack[-1] - stack[0]
#                 stack = [prices[i]]
#         return ans + stack[-1] - stack[0]# # 动态规划
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         dp = [[0] * 2 for _ in range(len(prices))]
#         dp[0][0] = -prices[0]
#         for i in range(1, len(prices)):
#             dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i])
#             dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])
#         return dp[-1][1]# 贪心
class Solution:def maxProfit(self, prices: List[int]) -> int:ans = 0for i in range(1, len(prices)):ans += max(prices[i] - prices[i - 1], 0)return ans

注意:
在上一题的只能进行一次股票买卖题中,因为股票全程只能买卖一次,所以如果买入股票,那么第i天持有股票即dp[i][0]一定就是 -prices[i]。而本题,因为一只股票可以买卖多次,所以当第i天买入股票的时候,所持有的现金可能有之前买卖过的利润。

dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]) # 可进行多次买卖的代码
dp[i][0] = max(dp[i - 1][0], - prices[i])  # 只能进行一次买卖的代码

买卖股票之规定买卖次数

class Solution:def maxProfit(self, prices: List[int]) -> int:dp = [[0] * 4 for _ in range(len(prices))]dp[0][0] = dp[0][2] = -prices[0]for i in range(1, len(prices)):dp[i][0] = max(dp[i - 1][0], -prices[i])dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] - prices[i])dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] + prices[i])return dp[-1][-1]
# 压缩
class Solution:def maxProfit(self, prices: List[int]) -> int:n = len(prices)dp = [0 for _ in range(4)]dp[0] = dp[2] = -prices[0]for i in range(1, n):dp[0] = max(dp[0], -prices[i])dp[1] = max(dp[1], dp[0] + prices[i])dp[2] = max(dp[2], dp[1] - prices[i])dp[3] = max(dp[3], dp[2] + prices[i])return dp[3]

买卖股票之可变的买卖次数

# 二维数组DP
class Solution:def maxProfit(self, k: int, prices: List[int]) -> int:if not prices:return 0n = len(prices)dp = [[0] * (2 * k + 1) for _ in range(n)]for i in range(1, 2*k + 1, 2):dp[0][i] = -prices[0]for i in range(1, n):for j in range(0, 2*k - 1, 2):dp[i][j+1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i])dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i])return dp[-1][-1]
# 压缩
class Solution:def maxProfit(self, k: int, prices: List[int]) -> int:if not prices:return 0n = len(prices)dp = [0] * (2 * k + 1) # 奇数时买入,偶数时卖出(除0外,0时表示没有进行任何操作)for i in range(1, 2*k + 1, 2):dp[i] = -prices[0]for i in range(1, n):for j in range(0, 2*k - 1, 2):dp[j + 1] = max(dp[j + 1], dp[j] - prices[i])dp[j + 2] = max(dp[j + 2], dp[j + 1] + prices[i])return dp[-1]# 创建buy和sell两个二维数组
class Solution:def maxProfit(self, k: int, prices: List[int]) -> int:if not prices:return 0n = len(prices)k = min(k, n // 2) # 剪枝,买卖的最大次数为prices的长度值的一半buy = [[0] * (k + 1) for _ in range(n)] # 由于k可能为1,即进行一次买卖,未来避免没有进行下面的嵌套循环,因此需要将k加一,此时下标0可以表示不进行操作时的值,下标k即表示第k(k>0)次买/卖后的值sell = [[0] * (k + 1) for _ in range(n)] #这两行的赋值操作不要写成一行buy = sell = [[0] * (k + 1) for _ in range(n)],否则答案错误!for i in range(k + 1):buy[0][i] = -prices[0]for i in range(1, n):for j in range(1, k + 1):buy[i][j] = max(buy[i - 1][j], sell[i - 1][j - 1] - prices[i])sell[i][j] = max(sell[i - 1][j], buy[i - 1][j] + prices[i])return max(sell[n - 1])
# 压缩
class Solution:def maxProfit(self, k: int, prices: List[int]) -> int:if not prices:return 0n = len(prices)k = min(k, n // 2)buy = [0] * (k + 1) sell = [0] * (k + 1) #这两行的赋值操作也不能写成一行,原理同上!for i in range(1, k + 1):  #为了便于理解,此处可以从1开始索引buy[i] = -prices[0]for i in range(1, n):for j in range(1, k + 1):buy[j] = max(buy[j], sell[j - 1] - prices[i])sell[j] = max(sell[j], buy[j] + prices[i])return sell[-1]

注意:

买卖股票之含冷冻期

class Solution:def maxProfit(self, prices: List[int]) -> int:if not prices: return 0n = len(prices)dp = [[0] * 3 for _ in range(n)]dp[0][0] = -prices[0]# dp[i][0]:手上持有股票的最大收益# dp[i][1]:手上不持有股票,并且处于冷冻期中的累计最大收益# dp[i][2]:手上不持有股票,并且不在冷冻期中的累计最大收益for i in range(1, n):dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] - prices[i])dp[i][1] = dp[i - 1][0] + prices[i]dp[i][2] = max(dp[i - 1][1], dp[i - 1][2])return max(dp[-1][1], dp[-1][2])class Solution:def maxProfit(self, prices: List[int]) -> int:if not prices: return 0n = len(prices)dp = [[0] * 3 for _ in range(n)]dp[0][0] = -prices[0]# dp[i][0]:买入# dp[i][1]:卖出(可能含冷冻期)# dp[i][2]:冷冻期for i in range(1, n):dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] - prices[i])dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])dp[i][2] = dp[i - 1][1]return dp[-1][1]

买卖股票之含手续费

class Solution:def maxProfit(self, prices: List[int], fee: int) -> int:if not prices: return 0n = len(prices)dp = [[0] * 2 for _ in range(n)]dp[0][0] = -prices[0]for i in range(1, n):dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i])dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee)return dp[-1][1]# 压缩
class Solution:def maxProfit(self, prices: List[int], fee: int) -> int:if not prices: return 0n = len(prices)dp = [-prices[0], 0]  for i in range(1, n):dp[0] = max(dp[0], dp[1] - prices[i])dp[1] = max(dp[1], dp[0] + prices[i] - fee)return dp[1]

Python Leetcode买卖股票相关推荐

  1. leetcode买卖股票问题(思路、方法、code)

    一文解决Leetcode买卖股票问题 对于前3个问题,均采用了比较巧妙的解法.由于第4个问题具有非常强的泛型,因此采用了DP,第4个问题的dp如果理解的话,实际上只需要稍加修改状态便可以用该dp思路应 ...

  2. LeetCode买卖股票之一:基本套路(122)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 关于<LeetCode买卖股票>系列 在L ...

  3. LeetCode买卖股票的最佳时机系列总结

    LeetCode买卖股票的最佳时机系列总结 此类动态规划从二维动规理解后优化到一维动规,部分题目还可以用到贪心. 目录: 121 买卖股票的最佳时机1 122 买卖股票的最佳时机2 123 买卖股票的 ...

  4. leetcode 买卖股票问题

    leetcode 买卖股票问题 lc121 买卖股票最佳时机 lc122 买卖股票最佳时机II lc123. 买卖股票的最佳时机 III lc188. 买卖股票的最佳时机 IV lc121 买卖股票最 ...

  5. LeetCode:121(Python)—— 买卖股票的最佳时机(简单)

    买卖股票的最佳时机 概述:给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格.你只能选择某一天买入这只股票,并选择在未来的某一个不同的日子卖出该股票 ...

  6. leetcode 买卖股票系列题目总结

    总结:买卖股票系列题目 1.买卖股票的最佳时机(简单) 121. 买卖股票的最佳时机 难度简单1093 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易( ...

  7. LeetCode买卖股票问题集合(六种股票问题)

    一.买卖股票的最佳时机 (LeetCode 121) 简单的动态规划问题,只能完成一次买卖,定义二维 dp[i] [] 数组,dp [i] [0]表示没有股票的状态,dp [i] [1]表示持有股票的 ...

  8. LeetCode 买卖股票的最佳时机 - 超详细讲解系列题

    1.分析 使用通用方法,也即动态规划DP (1)LeetCode 121. 买卖股票的最佳时机 class Solution {public int maxProfit(int[] prices) { ...

  9. LeetCode 买卖股票的最佳时机 6道题1个解法总结

    一个方法解决6道买卖股票题:来自LeetCode题解 一.穷举框架 利用「状态」进行穷举.我们具体到每一天,看看总共有几种可能的「状态」,再找出每个「状态」对应的「选择」.我们要穷举所有「状态」,穷举 ...

最新文章

  1. 【javascript】深入理解对象
  2. 专访腾讯云沙开波:从无到有,打造全球领先调度系统
  3. c++ socket 结构体
  4. python怎么创建文件夹视频_python怎么创建文件夹
  5. valid Palindrome -- leetcode
  6. Firefox UI已迁移至Web Components
  7. Restful Service 中 DateTime 在 url 中传递
  8. mysql multi主从复制_mysqld_multi方式配置Mysql数据库主从复制
  9. 如何从一张图片里取出其中一部分_如何鉴别坑人的锌合金龙头
  10. 开工了,为自己做的软件。先做些控件。
  11. Class yii\base\Exception
  12. 数值分析(5)-分段低次插值和样条插值
  13. LINUX下载编译libogg
  14. clone,Duplicate复制target XCode iOS
  15. 最新Hadoop环境搭建流程
  16. java压缩图片大小不改变图片分辨率
  17. 【洛谷】P1428:小鱼比可爱
  18. 浅谈跨站脚本攻击与防御
  19. 基于Java Web技术的动车购票系统
  20. PHPStorm中使用phpcs和php-cs-fixer

热门文章

  1. Chrome再出招 呈现API将仅支持HTTPS
  2. 易点租升级租赁品类 推“企业级”空气净化器配置方案
  3. rpm文件安装和卸载
  4. 关于嵌入式是前端还是后端
  5. 遥操作下 微创手术机器人 的 软件系统组成
  6. PS利用调整图层只需两步为偏黄肤色MM调出水嫩效果
  7. 上班族如何利用业余时间提升收入?
  8. 2017-ICCV-(DVF)Video Frame Synthesis using Deep Voxel Flow
  9. windows环境下启动mongodb服务
  10. 为什么要使用线程池?线程池有什么作用?