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] Best Time to Buy and Sell Stock IV

Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.Design...
  • u011331383
  • u011331383
  • 2015年02月23日 00:08
  • 8762

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求...
  • ljiabin
  • ljiabin
  • 2015年04月06日 11:10
  • 21746

关于LeetCode中Best Time to Buy and Sell Stock一题的理解

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If yo...
  • zsy112371
  • zsy112371
  • 2016年09月09日 13:55
  • 832

【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an...
  • u011613729
  • u011613729
  • 2014年07月24日 10:11
  • 3438

[leetcode 309]Best Time to Buy and Sell Stock with Cooldown--买卖股票with冷却期(DP法)

Question: Say you have an array for which the ith element is the price of a given stock on day ...
  • Xiaohei00000
  • Xiaohei00000
  • 2016年03月10日 23:02
  • 1753

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...
  • feliciafay
  • feliciafay
  • 2015年04月19日 15:45
  • 5474

leetcode【121+122+123 best time to buy and sell stock】【python】

我们先拿出来前三道题,因为他们都是array中的题目。这是leetcode种经典的一系列题,涉及到动态规划和贪心算法。按照我的理解,贪心是满足当前条件的最优值我们就将它最为最优解,也就是大家说的局部最...
  • u014251967
  • u014251967
  • 2016年09月12日 21:04
  • 797

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

题目如下: Best Time to Buy and Sell Stock Total Accepted: 43912 Total Submissions: 135635 My Submissions...
  • feliciafay
  • feliciafay
  • 2015年03月23日 08:37
  • 1953

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

乍看此题有难度,实则解法有规律。
  • u011613729
  • u011613729
  • 2015年08月14日 14:14
  • 1256

Leetcode算法学习日志-714 Best Time to Buy and Sell Stock with Transaction Fee

Leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee 题目原文 Your are given an array o...
  • Zarlove
  • Zarlove
  • 2017年10月23日 21:16
  • 1335
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Best Time to Buy and Sell Stock IV
举报原因:
原因补充:

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