Leetcode刷题188. 买卖股票的最佳时机 IV

给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入:k = 2, prices = [2,4,1]
输出:2
解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
示例 2:

输入:k = 2, prices = [3,2,6,5,0,3]
输出:7
解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
     随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

感谢liweiwei1419动态规划(「力扣」更新过用例,只有优化空间的版本可以 AC)labuladong一个方法团灭 6 道股票问题王尼玛四种解法+图解 188.买卖股票的最佳时机 IV

class Solution {
    public int maxProfit(int k, int[] prices) {
//		return maxProfitI(k, prices);
		return maxProfitII(k, prices);
//		return maxProfitIII(k, prices);
	}

	//方法三:递归+备忘录
	//时间复杂度O(kn),空间复杂度O(kn)
	private int maxProfitIII(int k, int[] prices) {
		if (prices == null || prices.length == 0 || k == 0) {
			return 0;
		}
		Map<String, Integer> memoMap = new HashMap<>();
		return dfs(prices, prices.length - 1, k, 0, memoMap);
	}

	private int dfs(int[] prices, int index, int k, int status, Map<String, Integer> memoMap) {
		if (index < 0 || k == 0) {
			return status == 1 ? Integer.MIN_VALUE : 0;
		}
		String key = index + "*" + k + "*" + status;
		if (memoMap.containsKey(key)) {
			return memoMap.get(key);
		}
		int res;
		if (status == 0) {
			res = Math.max(dfs(prices, index - 1, k, 0, memoMap),
					dfs(prices, index - 1, k, 1, memoMap) + prices[index]);
		} else {
			res = Math.max(dfs(prices, index - 1, k, 1, memoMap),
					dfs(prices, index - 1, k - 1, 0, memoMap) - prices[index]);
		}
		memoMap.put(key, res);
		return res;
	}

	//方法二:基于方法一空间优化,由于今天的状态值只参考了昨天的状态值,可以直接把第一维去掉。
	//时间复杂度O(kn),空间复杂度O(kn)
	private int maxProfitII(int k, int[] prices) {
		if (prices == null || prices.length == 0) {
			return 0;
		}
		int len = prices.length;
		if (k > len / 2) {
			return maxProfitWithInfiniteK(prices);
		}
		int[][] dp = new int[k + 1][2];
		for (int i = 1; i <= k; i++) {
			dp[i][0] = 0;
			dp[i][1] = -prices[0];
		}
		for (int price : prices) {
			for (int j = k; j >= 1; j--) {
				dp[j][1] = Math.max(dp[j][1], dp[j - 1][0] - price);
				dp[j][0] = Math.max(dp[j][0], dp[j][1] + price);
			}
		}
		return dp[k][0];
	}

	//方法一:动态规划
	//一次交易由买入和卖出构成,至少需要两天,所以有效的k应该不超过len/2,如果超过就没有约束作用了,相当于k等于无穷大,也就是第122题
	//时间复杂度O(kn),空间复杂度O(kn)
	private int maxProfitI(int k, int[] prices) {
		if (prices == null || prices.length == 0) {
			return 0;
		}
		int len = prices.length;
		if (k > len / 2) {
			//贪心算法
			return maxProfitWithInfiniteK(prices);
		}
		int[][][] dp = new int[len][k + 1][2];
		for (int i = 0; i <= k; i++) {
			dp[0][i][0] = 0;
			dp[0][i][1] = -prices[0];
		}
		for (int i = 1; i < len; i++) {
			for (int j = 1; j <= k; j++) {
				dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
				dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
			}
		}
		return dp[len - 1][k][0];
	}

	private int maxProfitWithInfiniteK(int[] prices) {
		int profit = 0;
		for (int i = 0; i < prices.length - 1; i++) {
			if (prices[i + 1] - prices[i] > 0) {
				profit += prices[i + 1] - prices[i];
			}
		}
		return profit;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值