Update 0714.买卖股票的最佳时机含手续费(动态规划).md

0714.买卖股票的最佳时机含手续费新增C语言实现
This commit is contained in:
a12bb
2024-03-12 21:39:58 +08:00
parent b6e458bcfa
commit 57e56c508b

View File

@ -247,7 +247,29 @@ function maxProfit(prices: number[], fee: number): number {
};
```
### C:
```c
#define max(a, b) ((a) > (b) ? (a) : (b))
// dp[i][0] 表示第i天持有股票所省最多现金。
// dp[i][1] 表示第i天不持有股票所得最多现金
int maxProfit(int* prices, int pricesSize, int fee) {
int dp[pricesSize][2];
dp[0][0] = -prices[0];
dp[0][1] = 0;
for (int i = 1; i < pricesSize; ++i) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
}
return dp[pricesSize - 1][1];
}
```
### Rust:
**贪心**
```Rust
@ -304,3 +326,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>