当前位置:网站首页>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 profit2. 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 set process startup and self startup

1090.Phone List

从0到1完全掌握 XSS

Arithmetic operations and expressions

C language escape character and its meaning

Learning notes on February 5, 2022 (C language)

【Try to Hack】vulnhub DC1

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

Std:: vector minutes

Use Matplotlib to draw a line chart
随机推荐
Daily question, Caesar code,
[C language] 32 keyword memory skills
Is it safe to open a stock account online?
NBD Network Block Device
RDB and AOF persistence of redis
QT database connection
How to package rpm
14 -- 验证回文字符串 Ⅱ
Qlogsystem log system configuration use
买卖股票的最佳时机
Basic knowledge of pointer
Generation method and usage of coredump
Golang channel reading data
BM setup process
Std:: vector minutes
Modal and modeless dialogs for QT
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
One question per day, punch in
Flexible layout (display:flex;) Attribute details
Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)