添加0714.买卖股票的最佳时机含手续费和Java版本

This commit is contained in:
wzs
2021-05-12 23:26:17 +08:00
parent cbc7efa1b2
commit e476ac9587

View File

@ -154,9 +154,31 @@ public:
## 其他语言版本
Java
```java
class Solution { // 动态规划
public int maxProfit(int[] prices, int fee) {
if (prices == null || prices.length < 2) {
return 0;
}
int[][] dp = new int[prices.length][2];
// bad case
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[prices.length - 1][0];
}
}
```
Python