当前位置:网站首页>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 .边栏推荐
- Yolov3 spp Darknet version to caffemodel and then to OM model
- Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
- Boost listening port server
- Single user mode
- Introduction to flexible array
- Vs2019 scanf error
- Explanation of dev/mapper
- 5 connection modes of QT signal slot
- Use Matplotlib to draw a line chart
- 14 -- validate palindrome string II
猜你喜欢

Source code analysis of synergetics and ntyco

Master XSS completely from 0 to 1

Sequential programming 1

google_ Breakpad crash detection

Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat

How to cut the size of a moving picture? Try this online photo cropping tool

Qcodeeditor - QT based code editor

Fishing detection software

Character encoding minutes

定位position(5种方式)
随机推荐
Luogu p5707 [deep foundation 2. example 12] late for school
Dynamic memory allocation
Design and implementation of timer
High precision addition
The difference between sizeof and strlen
QT inline dialog
Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
From 408 to independent proposition, 211 to postgraduate entrance examination of Guizhou University
5 connection modes of QT signal slot
GDB debugging
NBD Network Block Device
14 -- 验证回文字符串 Ⅱ
QT loading third-party library basic operation
Study notes of cmake
HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
QQ love talk candy love talk content acquisition and storage
电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备
[untitled] PTA check password
What moment makes you think there is a bug in the world?
@Font face fonts only work on their own domain - @font-face fonts only work on their own domain