Merge branch 'youngyangyang04:master' into master

This commit is contained in:
kimoji
2024-07-17 08:59:04 +08:00
committed by GitHub
2 changed files with 22 additions and 2 deletions

View File

@ -466,7 +466,7 @@ function maxProfit(prices: number[]): number {
}; };
``` ```
> 动态规划 > 动态规划:版本一
```typescript ```typescript
function maxProfit(prices: number[]): number { function maxProfit(prices: number[]): number {
@ -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# ### C#
> 贪心法 > 贪心法

View File

@ -87,7 +87,7 @@
回归本题整个插入过程如下 回归本题整个插入过程如下
排序完的people 排序完的people
[[7,0], [7,1], [6,1], [5,0], [5,2][4,4]] [[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]
插入的过程 插入的过程
* 插入[7,0][[7,0]] * 插入[7,0][[7,0]]