当前位置:网站首页>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多状态,行吧
边栏推荐
- gcd最大公约数
- Using elk to build a log analysis system (I) -- component introduction
- 英语小记 - 表因果
- 《性能之巅第2版》阅读笔记(二)--CPU监测
- 【MySQL】多表连接查询
- 02 mongodb data types, important concepts and common shell instructions
- Visualization of loss using tensorboard
- How to learn a programming language systematically| Dark horse programmer
- 03 MongoDB文档的各种增加、更新、删除操作总结
- 多项目设计开发·类库项目引入入门
猜你喜欢

《性能之巅第2版》阅读笔记(二)--CPU监测

From zero to one, I will teach you to build a "search by text and map" search service (I)

In the era of video explosion, who is supporting the high-speed operation of video ecological network?

机器学习入门笔记

MySQL 主从复制、分离解析

Adder - Notes

@Several scenarios of transactional failure

One article tells you what kubernetes is

Chapter 14 AC-DC power supply front stage circuit note I

第一章 Bash 入门
随机推荐
《性能之巅第2版》阅读笔记(二)--CPU监测
05 MongoDB对列的各种操作总结
回溯—迷宫问题
加法器—笔记
Does the applet input box flash?
Staggered and permutation combination formula
数字电路学习笔记(二)
内卷、躺平与中年危机的相关思考
用一个栈实现另一个栈的排序
Learning notes of digital circuit (II)
数字电路学习笔记(一)
揭开SSL的神秘面纱,了解如何用SSL保护数据
公司领导说,个人代码超10个Bug就开除,是什么体验?
品达通用权限系统(Day 5~Day 6)
Talking about cloud primitiveness, we have to talk about containers
软件测试报告怎么编写?第三方性能报告范文模板来了
Building a server monitoring platform with telegraf influxdb grafana
第14章 AC-DC电源前级电路 笔记一
简单工厂模式
GCD maximum common divisor