LeetCode Best Time to Buy and Sell Stock IV

原创 2015年09月06日 06:08:09

Description:

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Solution:

We may use dp[i][l] to represent that we can get the most money before day i and within k transactions.

So we can easily get the transformation equation:

dp[i][l] = max { dp[i-1][l] , dp[i][l-1], max{ dp[j-1][l-1] + prices[i] - prices[j] } }

We may as well make a little change in this equation:

dp[j-1][l-1] + prices[i] - prices[j] = dp[j-1][l-1] - prices[j] + prices[i]

And we can use one variable to keep the value of { dp[j-1][l-1] - prices[j] }. Just be aware that we need to change the loop of k in the outsider, and the loop of prices inside in order to realize this {dp[j-1][l-1] - prices[j]} variable. So the time complexity will reduce from O(n^2 * k) to O(n*k).

But also pay attention that there are still some test cases that cannot pass, which has a very large k. The solution to this is very simple, every time 2*k >= n, it is obvious that we can add all the adjacent increasing number.

import java.util.*;

public class Solution {

	public int maxProfit(int k, int[] prices) {
		int len = prices.length;
		if (len == 0)
			return 0;
		if (k * 2 >= len) {
			int ans = 0;
			for (int i = 1; i < len; i++)
				if (prices[i] > prices[i - 1])
					ans += prices[i] - prices[i - 1];
			return ans;
		}

		int dp[][] = new int[len][k + 1];

		int temp = 0;
		// max { dp[j-1][k-1] + prices[i] - prices[j] }
		// temp = max dp[j-1][k-1] - prices[j]
		for (int l = 1; l <= k; l++) {
			temp = -prices[0];
			for (int i = 1; i < len; i++) {
				dp[i][l] = Math.max(dp[i - 1][l], dp[i][l - 1]);
				dp[i][l] = Math.max(dp[i][l], prices[i] + temp);
				temp = Math.max(temp, dp[i - 1][l - 1] - prices[i]);
			}
		}

		int max = 0;
		for (int i = 0; i <= k; i++)
			max = Math.max(max, dp[len - 1][i]);
		return max;
	}

	public static void main(String[] args) {
		int arr[] = new int[] { 2, 1, 2, 0, 1 };
		Solution s = new Solution();
		System.out.println(s.maxProfit(2, arr));
	}
}


leetcode 188---Best Time to Buy and Sell Stock IV

问题描述:Say you have an array for which the ith element is the price of a given stock on day i. 给你一个数组...
  • will130
  • will130
  • 2015年05月29日 13:36
  • 266

LeetCode--Best Time to Buy and Sell Stock IV(DP + 滚动数组)

题目:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/

LeetCode(188) Best Time to Buy and Sell Stock IV (Java)

题目如下: Best Time to Buy and Sell Stock Total Accepted: 43912 Total Submissions: 135635 My Submissions...

leetcode_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV

121. Best Time to Buy and Sell Stock [题目] Say you have an array for which the ith element is the p...

Best Time to Buy and Sell Stock IV-- LeetCode

这里我们先解释最多可以进行k次交易的算法,然后最多进行两次我们只需要把k取成2即可。我们还是使用“局部最优和全局最优解法”。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(...

leetcode -- Best Time to Buy and Sell Stock IV --难点要看

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/参考: http://bookshadow.com/weblog/2...
  • xyqzki
  • xyqzki
  • 2015年12月28日 18:34
  • 287

Leetcode227: Best Time to Buy and Sell Stock IV

Say you have an array for which the ith element is the price of a given stock on day i. Design an...

leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

leetcode第188题,Best Time to Buy and Sell Stock IV题目如下: https://leetcode.com/problems/best-time-to-buy...

leetcode笔记:Best Time to Buy and Sell Stock IV

这道题是之前买卖股票题目二和三的综合体,但情况更为复杂,允许进行k次买卖,k的值显然是可变的。该题需使用动态规划来完成。...

【LeetCode】Best Time to Buy and Sell Stock IV 动态规划dp解法(C++)

乍看此题有难度,实则解法有规律。
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Best Time to Buy and Sell Stock IV
举报原因:
原因补充:

(最多只允许输入30个字)