mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
update 0121 and 0122
This commit is contained in:
@ -464,6 +464,47 @@ function maxProfit(prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
C#:
|
||||||
|
|
||||||
|
> 贪心法
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int MaxProfit(int[] prices)
|
||||||
|
{
|
||||||
|
int min = Int32.MaxValue;
|
||||||
|
int res = 0;
|
||||||
|
for (int i = 0; i < prices.Length; i++)
|
||||||
|
{
|
||||||
|
min = Math.Min(prices[i], min);
|
||||||
|
res = Math.Max(prices[i] - min, res);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 动态规划
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int MaxProfit(int[] prices)
|
||||||
|
{
|
||||||
|
int[] dp = new int[2];
|
||||||
|
int size = prices.Length;
|
||||||
|
(dp[0], dp[1]) = (-prices[0], 0);
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
dp[0] = Math.Max(dp[0], -prices[i]);
|
||||||
|
dp[1] = Math.Max(dp[1], dp[0]+prices[i]);
|
||||||
|
}
|
||||||
|
return dp[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -471,3 +512,4 @@ function maxProfit(prices: number[]): number {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -169,8 +169,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
> 版本一:
|
> 版本一:
|
||||||
@ -253,8 +251,6 @@ func max(a,b int)int{
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Javascript:
|
Javascript:
|
||||||
```javascript
|
```javascript
|
||||||
// 方法一:动态规划(dp 数组)
|
// 方法一:动态规划(dp 数组)
|
||||||
@ -331,9 +327,47 @@ function maxProfit(prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
C#:
|
||||||
|
|
||||||
|
> 贪心法
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int MaxProfit(int[] prices)
|
||||||
|
{
|
||||||
|
int res = 0;
|
||||||
|
for (int i = 1; i < prices.Length; i++)
|
||||||
|
res += Math.Max(0, prices[i] - prices[i-1]);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 动态规划
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int MaxProfit(int[] prices)
|
||||||
|
{
|
||||||
|
int[] dp = new int[2];
|
||||||
|
dp[0] = -prices[0];
|
||||||
|
|
||||||
|
for (int i = 1; i < prices.Length; i++)
|
||||||
|
{
|
||||||
|
dp[0] = dp[0]>dp[1] - prices[i]?dp[0]:dp[1] - prices[i];
|
||||||
|
dp[1] = dp[1] > dp[0] + prices[i] ? dp[1] : dp[0] + prices[i];
|
||||||
|
}
|
||||||
|
return dp[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user