当前位置:网站首页>Leetcode122 timing of buying and selling stocks II
Leetcode122 timing of buying and selling stocks II
2022-06-25 15:10:00 【Milanien】
difficulty : secondary
Title Description
Ideas
1. The law of greed ( Add up the price difference of all price rising ranges , The calculation process is the actual transaction process )
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
i = 0
while i < (len(prices) - 1):
minprice = prices[i]
maxprice = prices[i]
j = i + 1
while j < len(prices) and prices[j] > maxprice:
maxprice = prices[j]
j += 1
profit += max(0, maxprice-minprice)
i = j
#print("j:",j,"profit:",profit)
return profit
2. Dynamic programming ( Refer to the official answer )https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-ii-by-leetcode-s/
1) Define the State
dp[i][0]: The first i There is no maximum profit of stocks after trading for days
dp[i][1]: The first i The maximum profit of holding stocks after days of trading
2) Transfer equation
Ruodi i No stock , Then the maximum profit is i-1 The biggest profit when there is no stock , Or the third i-1 One day there are stocks , The first i The biggest profit from selling in one day . The first i Days to sell to get prices[i] The profits of the .
dp[i][0]=max{dp[i−1][0],dp[i−1][1]+prices[i]}
Ruodi i One day there are stocks , Then the maximum profit is i-1 The biggest profit when there are shares in the sky , Or the third i-1 No stock , The first i The biggest profit of day buying . The first i Day buying decreases prices[i] The profits of the .
dp[i][1]=max{dp[i−1][1],dp[i−1][0]−prices[i]}
3) The initial state
dp[0][0]=0,dp[0][1]=-prices[0]
4) Code
class Solution:
def maxProfit(self, prices: List[int]) -> int:
dp0 = 0
dp1 = -prices[0]
for i in range(len(prices)):
newdp0 = max(dp0, dp1+prices[i])
newdp1 = max(dp1, dp0-prices[i])
dp0 = newdp0
dp1 = newdp1
return dp0
边栏推荐
- QT pop up open file dialog box QFileDialog
- Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
- Software packaging and deployment
- Stderr and stdout related standard outputs and other C system APIs
- 有哪个瞬间让你觉得这个世界出bug了?
- How to make GIF animation online? Try this GIF online production tool
- 55 specific ways to improve program design (2)
- QT database connection deletion
- One code per day - day one
- 搭建极简GB28181 网守和网关服务器,建立AI推理和3d服务场景,然后开源代码(一)
猜你喜欢
有哪个瞬间让你觉得这个世界出bug了?
多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
QT pattern prompt box implementation
Fishing detection software
Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried
C language escape character and its meaning
QT set process startup and self startup
90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟
Arithmetic operations and expressions
随机推荐
14 -- validate palindrome string II
C language escape character and its meaning
JS capture, target, bubble phase
Source code analysis of synergetics and ntyco
90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟
[deep learning] multi task learning of multiple datasets data sets missing labels
Learning notes on February 8, 2022 (C language)
(translation) json-rpc 2.0 specification (Chinese version)
Customization and encapsulation of go language zap library logger
How to cut the size of a moving picture? Try this online photo cropping tool
QQ love talk candy love talk content acquisition and storage
iconv_ Open returns error code 22
Esp8266 building smart home system
User defined data type - structure
Biscuit distribution
Qt: Pro project file
What moment makes you think there is a bug in the world?
Study notes of cmake
RDB and AOF persistence of redis
Arithmetic operations and expressions