309. 买卖股票的最佳时机含冷冻期 - 力扣(LeetCode)
- public class Solution {
-
- public int maxProfit(int[] prices) {
- int len = prices.length;
- if (len < 2) {
- return 0;
- }
-
- int[] dp = new int[3];
-
- dp[0] = 0;
- dp[1] = -prices[0];
- dp[2] = 0;
-
- int pre0 = dp[0];
- int pre2 = dp[2];
-
- for (int i = 1; i < len; i++) {
- dp[0] = Math.max(dp[0], pre2);
- dp[1] = Math.max(dp[1], pre0 - prices[i]);
- dp[2] = dp[1] + prices[i];
-
- pre0 = dp[0];
- pre2 = dp[2];
- }
- return Math.max(dp[0], dp[2]);
- }
- }
-
714. 买卖股票的最佳时机含手续费 - 力扣(LeetCode)
- class Solution {
- public int maxProfit(int[] prices, int fee) {
- int maximumProfit = 0;
- int n = prices.length;
- int prevValue = prices[0] + fee;
- for (int i = 1; i < n; i++) {
- if (prices[i] + fee < prevValue) {
- prevValue = prices[i] + fee;
- } else if (prices[i] > prevValue) {
- maximumProfit += prices[i] - prevValue;
- prevValue = prices[i];
- }
- }
- return maximumProfit;
- }
- }
-
