mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Update 0714.买卖股票的最佳时机含手续费(动态规划).md
0714.买卖股票的最佳时机含手续费新增C语言实现
This commit is contained in:
@ -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>
|
||||
|
||||
|
Reference in New Issue
Block a user