当前位置:网站首页>leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
2022-06-28 04:12:00 【Review of the white speed Dragon King】

analysis
dp[i][0] It means the first one i No stock in positions max profits ,dp[i][1] It means the first one i There are stocks in positions max profits
The first 0 One of the dp Namely [0, -prices[0]]
then dp[i][0] yes max( There was no stock continuation before , There were shares before but they were sold )
then dp[i][1] yes max( There was a continuation of the stock , I didn't have any stocks before, but I bought )
ac code
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
# dp[i][0]: The first i There is no stock's maximum profit
# dp[i][1]: The first i Tianyou stock's maximum profit
# Two states
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]
summary
dp Two states
Interact with each other
Another dp Multistate , All right
边栏推荐
- Building log analysis system with elk (III) -- Security Authentication
- Web APIs DOM event foundation dark horse programmer
- How to learn a programming language systematically| Dark horse programmer
- 错排兼排列组合公式
- Notes to friendship chain
- From zero to one, I will teach you to build a "search by text and map" search service (I)
- 歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?
- 05 mongodb summary of various column operations
- After launching the MES system, these changes have taken place in the enterprise
- 软件测试报告怎么编写?第三方性能报告范文模板来了
猜你喜欢

Web APIs DOM event foundation dark horse programmer

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

Pychart shares third-party modules among different projects

Detailed explanation of iptables firewall rules and firewalld firewall rules

How to write a software test report? Here comes the third party performance report template

Learning notes of digital circuit (II)

One article tells you what kubernetes is

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

Does the applet image component not display pictures?

ambari SSLError: Failed to connect. Please check openssl library versions.
随机推荐
MSC 307(88) (2010 FTPC Code)第2部分烟气和毒性测试
MSC 307(88) (2010 FTPC Code) Part 5低播焰测试
Single responsibility principle
有关函数模板的那些小知识-.-
leetcode - 329. 矩阵中的最长递增路径
04 MongoDB各种查询操作 以及聚合操作总结
MSc 307 (88) (2010 FTPC code) Part 2 smoke and toxicity test
How to apply for ASTM E108 flame retardant test for photovoltaic panels?
Market competitiveness of robot programming education
Building log analysis system with elk (III) -- Security Authentication
抖音实战~关注博主
Reverse a stack with recursive functions and stack operations only
多项目设计开发·类库项目引入入门
揭开SSL的神秘面纱,了解如何用SSL保护数据
多项目开发入门,基础设计 类库项目使用
Staggered and permutation combination formula
Iso8191 test is mentioned in as 3744.1. Are the two tests the same?
Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
[small program practice series] e-commerce platform source code and function implementation
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?