当前位置:网站首页>leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
2022-06-28 03:40:00 【白速龙王的回眸】

分析
dp[i][0]表示第i个位置没股票max利润,dp[i][1]表示第i个位置有股票max利润
第0个的dp就是[0, -prices[0]]
然后dp[i][0]是max(之前没有股票的延续,之前有股票但卖了)
然后dp[i][1]是max(之前有股票的延续,之前没有股票但买了)
ac code
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
# dp[i][0]: 第i天没有股票的最大利润
# dp[i][1]: 第i天有股票的最大利润
# 双状态
n = len(prices)
dp = [[0, -prices[0]]] + [[0, 0] for _ in range(n - 1)]
for i in range(1, n):
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee)
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i])
return dp[-1][0]
总结
dp双状态
互相影响
又是一个dp多状态,行吧
边栏推荐
- A Preliminary Study of Blackbody radiation
- 窗帘做EN 1101易燃性测试过程是怎么样的?
- 用一个栈实现另一个栈的排序
- Two methods of shell script parameter passing based on arm5718
- Open the field of maker education and creation
- 从零到一,教你搭建「以文搜图」搜索服务(一)
- leetcode:单调栈结构(进阶)
- 04 summary of various query operations and aggregation operations of mongodb
- MSC 307(88) (2010 FTPC Code) Part 5低播焰测试
- How to apply for ASTM E108 flame retardant test for photovoltaic panels?
猜你喜欢

关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)

Analysis of future teacher research ability under steam education framework

sqlserver 数据库之事物使用入门 案例

数字电路学习笔记(一)

【小程序实战系列】电商平台源码及功能实现

歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?

从零到一,教你搭建「以文搜图」搜索服务(一)

The operating mechanism of spectrogram in audio Science

多项目设计开发·类库项目引入入门

MySQL master-slave replication, separation and resolution
随机推荐
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
Pychart shares third-party modules among different projects
窗帘做EN 1101易燃性测试过程是怎么样的?
歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?
Introduction notes to machine learning
03 MongoDB文档的各种增加、更新、删除操作总结
How to write a software test report? Here comes the third party performance report template
Analysis of future teacher research ability under steam education framework
A summary of my recent situation in June 2022
音频 scipy 中 spectrogram 的运作机制
多项目开发入门,基础设计 类库项目使用
A solution to the inefficiency of setting debug mode in developing flask framework with pychar
机器学习入门笔记
MSC 307(88) (2010 FTPC Code) Part 9床上用品试验
Reverse a stack with recursive functions and stack operations only
Reading notes of top performance version 2 (II) -- Performance observation tool
电学基础知识整理(二)
GCD maximum common divisor
《性能之巅第2版》阅读笔记(二)--性能观察工具
等保三级密码复杂度是多少?多久更换一次?