mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0122.买卖股票的最佳时机II.md
This commit is contained in:
@ -274,6 +274,7 @@ const maxProfit = (prices) => {
|
||||
|
||||
### TypeScript:
|
||||
|
||||
贪心
|
||||
```typescript
|
||||
function maxProfit(prices: number[]): number {
|
||||
let resProfit: number = 0;
|
||||
@ -284,6 +285,21 @@ function maxProfit(prices: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
动态规划
|
||||
```typescript
|
||||
function maxProfit(prices: number[]): number {
|
||||
const dp = Array(prices.length)
|
||||
.fill(0)
|
||||
.map(() => Array(2).fill(0))
|
||||
dp[0][0] = -prices[0]
|
||||
for (let i = 1; i < prices.length; i++) {
|
||||
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i])
|
||||
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i])
|
||||
}
|
||||
return dp[prices.length - 1][1]
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
贪心:
|
||||
|
Reference in New Issue
Block a user