mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update 0188.买卖股票的最佳时机IV.md
0188.买卖股票的最佳时机IV新增C语言实现
This commit is contained in:
@ -474,6 +474,34 @@ function maxProfit(k: number, prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### C:
|
||||||
|
|
||||||
|
```c
|
||||||
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||||
|
|
||||||
|
int maxProfit(int k, int* prices, int pricesSize) {
|
||||||
|
if(pricesSize == 0){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int dp[pricesSize][2 * k + 1];
|
||||||
|
memset(dp, 0, sizeof(int) * pricesSize * (2 * k + 1));
|
||||||
|
for (int j = 1; j < 2 * k; j += 2) {
|
||||||
|
dp[0][j] = -prices[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1;i < pricesSize; i++) {//枚举股票
|
||||||
|
for (int j = 0; j < 2 * k - 1; j += 2) { //更新每一次买入卖出
|
||||||
|
dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);
|
||||||
|
dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[pricesSize - 1][2 * k];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Rust:
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -529,3 +557,4 @@ 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