Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
jenningsloy318 merged 4 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update 121. 买卖股票的最佳时机.md
  • Loading branch information
Monster-XU-jesus committed Jul 15, 2024
commit afbcb1d98bd5a0e40f9f65d1ced60e62c286fbf2
22 changes: 21 additions & 1 deletion problems/0121.买卖股票的最佳时机.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ function maxProfit(prices: number[]): number {
};
```

> 动态规划
> 动态规划:版本一

```typescript
function maxProfit(prices: number[]): number {
Expand All @@ -487,6 +487,26 @@ function maxProfit(prices: number[]): number {
};
```

> 动态规划:版本二

```typescript
// dp[i][0] 表示第i天持有股票所得最多现金
// dp[i][1] 表示第i天不持有股票所得最多现金
function maxProfit(prices: number[]): number {
const dp:number[][] = Array(2).fill(0).map(item => Array(2));
dp[0][0] = -prices[0];
dp[0][1] = 0;

for (let i = 1; i < prices.length; i++) {
dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], -prices[i]);
dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i]);
}

// 返回不持有股票的最大现金
return dp[(prices.length-1) % 2][1];
};
```

### C#:

> 贪心法
Expand Down

AltStyle によって変換されたページ (->オリジナル) /