Skip to content

Commit 3f9dc14

Browse files
committed
chore: add daily leetcode post [121]买卖股票的最佳时期_translated
1 parent 80b334c commit 3f9dc14

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: 121.The best time for buying and selling stocks
3+
date: '2024.01.01 0:00'
4+
tags:
5+
- Python
6+
- answer
7+
- Array
8+
- Dynamic planning
9+
abbrlink: 3a21fe32
10+
---
11+
# topic:
12+
13+
14+
[2251.The number of flowers during the flowering period.md](https://leetcode-cn.com/problems/number-of-flowers-that-can-be-planted-in-garden/)
15+
# Thought:
16+
17+
Dynamic planning,Find the least problem
18+
Minority problem:FirstiThe maximum profit of heaven = max(Firsti-1The maximum profit of heaven, FirstiHeavenly price - forwardi-1The minimum price of heaven)
19+
20+
# Code:
21+
```python
22+
class Solution:
23+
def maxProfit(self, prices: List[int]) -> int:
24+
# Minority problem:FirstiThe maximum profit of heaven = max(Firsti-1The maximum profit of heaven, FirstiHeavenly price - forwardi-1The minimum price of heaven)
25+
dp = [0] * len(prices)
26+
min_price = prices[0]
27+
for i in range(1, len(prices)):
28+
dp[i] = max(dp[i - 1], prices[i] - min_price)
29+
min_price = min(min_price, prices[i])
30+
return dp[-1]
31+
```

0 commit comments

Comments
 (0)