mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update 0309.最佳买卖股票时机含冷冻期.md
0309.最佳买卖股票时机含冷冻期新增C语言实现
This commit is contained in:
@ -457,6 +457,40 @@ function maxProfit(prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### C:
|
||||||
|
|
||||||
|
```c
|
||||||
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 状态一:持有股票状态(今天买入股票,
|
||||||
|
* 或者是之前就买入了股票然后没有操作,一直持有)
|
||||||
|
* 不持有股票状态,这里就有两种卖出股票状态
|
||||||
|
* 状态二:保持卖出股票的状态(两天前就卖出了股票,度过一天冷冻期。
|
||||||
|
* 或者是前一天就是卖出股票状态,一直没操作)
|
||||||
|
* 状态三:今天卖出股票
|
||||||
|
* 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天!
|
||||||
|
|
||||||
|
*/
|
||||||
|
int maxProfit(int* prices, int pricesSize) {
|
||||||
|
if(pricesSize == 0){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int dp[pricesSize][4];
|
||||||
|
memset(dp, 0, sizeof (int ) * pricesSize * 4);
|
||||||
|
dp[0][0] = -prices[0];
|
||||||
|
for (int i = 1; i < pricesSize; ++i) {
|
||||||
|
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1] - prices[i], dp[i - 1][3] - prices[i]));
|
||||||
|
dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
|
||||||
|
dp[i][2] = dp[i - 1][0] + prices[i];
|
||||||
|
dp[i][3] = dp[i - 1][2];
|
||||||
|
}
|
||||||
|
return max(dp[pricesSize - 1][1], max(dp[pricesSize - 1][2], dp[pricesSize - 1][3]));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Rust:
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -486,4 +520,3 @@ impl Solution {
|
|||||||
<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