当前位置:网站首页>Leetcode123 timing of buying and selling stocks III
Leetcode123 timing of buying and selling stocks III
2022-06-25 15:10:00 【Milanien】
difficulty : difficult
Title Description
Ideas
Dynamic programming
1) Define the State
No operation ; Buy only once buy1; Carry out a buy and sell operation , That is to complete a transaction sell1; Make a second purchase buy2; Complete two transactions sell2.
2) Transfer equation
The first i Days without operation , Or i-1 Days without operation , The first i Heaven and earth prices[i] Buy stock at a price
buy1=max{buy1′,−prices[i]}=max{buy1,−prices[i]}
The first i Days without operation , Or i-1 Only buy everyday , The first i Heaven and earth prices[i] Sell shares at a price
sell1=max{sell1′,buy1′+prices[i]}=max{sell1,buy1+prices[i]}
The first i Days without operation , Or i-1 Complete a transaction in days , The first i Heaven and earth prices[i] Buy stock at a price
buy2=max{buy2′,sell1′−prices[i]}=max{buy2,sell1−prices[i]}
The first i Days without operation , Or i-1 Day for the second time to buy , The first i Heaven and earth prices[i] Sell shares at a price
sell2=max{sell2′,buy2′+prices[i]}=max{sell2,buy2+prices[i]}
3) The initial state
buy1=−prices[0];sell1=0;buy2=−prices[0];sell2=0
Code
class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
buy1 = buy2 = -prices[0]
sell1 = sell2 = 0
for i in range(1, n):
buy1 = max(buy1, -prices[i])
sell1 = max(sell1, buy1 + prices[i])
buy2 = max(buy2, sell1 - prices[i])
sell2 = max(sell2, buy2 + prices[i])
return sell2
author :LeetCode-Solution
link :https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-iii-by-wrnt/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .边栏推荐
- AB string interchange
- Single user mode
- Bessie's weight problem [01 backpack]
- Judging the number of leap years from 1 to N years
- [deep learning] multi task learning of multiple datasets data sets missing labels
- RDB and AOF persistence of redis
- Semaphore function
- QT pop up open file dialog box QFileDialog
- Real variable instance
- 从0到1完全掌握 XSS
猜你喜欢

Fishing detection software

In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried

1090.Phone List
![[try to hack] vulhub shooting range construction](/img/fc/6057b6dec9b51894140453e5422176.png)
[try to hack] vulhub shooting range construction

QQ情话糖果情话内容获取并保存

【Try to Hack】vulnhub DC1

What moment makes you think there is a bug in the world?

Dynamic memory allocation

Learning notes on February 18, 2022 (C language)

Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
随机推荐
QT file reading -qfile
Gif动画怎么在线制作?快试试这款gif在线制作工具
(translation) json-rpc 2.0 specification (Chinese version)
Gif动图如何裁剪?收下这个图片在线裁剪工具
55 specific ways to improve program design (1)
JS select all exercise
Two advanced playing methods of QT signal and slot
Biscuit distribution
In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried
搭建极简GB28181 网守和网关服务器,建立AI推理和3d服务场景,然后开源代码(一)
Boost listening port server
Mining procedure processing
[untitled] PTA check password
‘make_ unique’ is not a member of ‘std’
A deformation problem of Hanoi Tower
High precision addition
Go语言Zap库Logger的定制化和封装使用详解
90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟
For the first time in China, Chinatelecom 5g underground personnel positioning project is officially commercial: it can track the position in real time to ensure operation safety
JS capture, target, bubble phase