当前位置:网站首页>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
边栏推荐
- QQ情话糖果情话内容获取并保存
- Learning C language today is the first time to learn C language. In college, C linguistics is not good, but I want to make progress, so I found a beep video on the Internet to learn C language
- QT article outline
- Dynamic memory allocation
- ‘make_ unique’ is not a member of ‘std’
- QQ love talk candy love talk content acquisition and storage
- Master XSS completely from 0 to 1
- Is it normal to dig for money? Is it safe to open a stock account?
- Vs2019 scanf error
- 【Try to Hack】vulnhub DC1
猜你喜欢

Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment

Design and implementation of timer

Design and implementation of thread pool

Qcodeeditor - QT based code editor

Introduction to flexible array

Source code analysis of zeromq lockless queue

QQ love talk candy love talk content acquisition and storage

Yolov3 spp Darknet version to caffemodel and then to OM model

Sequential programming 1

多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
随机推荐
C language LNK2019 unresolved external symbols_ Main error
dev/mapper的解释
Solution of push code failure in idea
How to cut the size of a moving picture? Try this online photo cropping tool
Gif动画怎么在线制作?快试试这款gif在线制作工具
Software packaging and deployment
【Try to Hack】vulnhub DC1
Arithmetic operations and expressions
Is it safe to open a stock account online?
Open a restaurant
Stderr and stdout related standard outputs and other C system APIs
Introduction to flexible array
Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
p1408
2. operator and expression multiple choice questions
In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried
Custom structure type
Review of arrays and pointers triggered by a topic
Learning notes on February 5, 2022 (C language)
Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password