[LeetCode]Best Time to Buy and Sell Stock III 动态规划

本题是Best Time to Buy and Sell Stock/的改进版。

本题中,可以买最多买进卖出两次股票。

买两次股票可以看成是第0~i天买进卖出以及第i+1~n-1天买进卖出两部分。这要枚举i并求出0th~ith的最大利益与(i+1)th~(n-1)th的最大利益之和的最大值就是买进卖出两次可以得到的最大利益。即状态转移方程:

dp[0,n-1]=max{dp[0,k]+dp[k+1,n-1]},k=1,...,n-2

而只买进卖出一次的最大利润通过0th~ith可以求得。

这里求(i+1)th~(n-1)th的最大利润可以由右向左遍历的方法实现。

class Solution {
public:
	int maxProfit(vector<int> &p) {
		int n=p.size(),i,j;
		if(n<=1)return 0;
        int *le=new int[n],*ri=new int[n];
		memset(le,0,sizeof(int)*n);
		memset(ri,0,sizeof(int)*n);
		int minv=p[0];
		for(i=1;i<n;++i){
			if(p[i]-minv>0)le[i]=p[i]-minv;
			if(le[i]<le[i-1])le[i]=le[i-1];
			if(minv>p[i])minv=p[i];
		}
		int maxv=p[n-1];
		for(i=n-2;i>=0;--i){
			if(maxv-p[i] >0)ri[i]=maxv-p[i];
			if(ri[i]<ri[i+1])ri[i]=ri[i+1];
			if(maxv<p[i])maxv=p[i];
		}
		maxv=le[n-1];
		for(i=1;i<n-2;++i){
			if(le[i]+ri[i+1]>maxv)maxv=le[i]+ri[i+1];
		}
		return maxv;
    }
};
时间: 2024-08-05 06:39:30

[LeetCode]Best Time to Buy and Sell Stock III 动态规划的相关文章

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

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 two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode: Best Time to Buy and Sell Stock III [123]

[题目] 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 two transactions. Note: You may not engage in multiple transactions at the same time

LeetCode——Best Time to Buy and Sell Stock III

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 two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode: Best Time to Buy and Sell Stock III 解题报告

Best Time to Buy and Sell Stock IIIQuestion SolutionSay 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 two transactions. Note:You may not en

[Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (ie

[LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

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 two transactions. Note:You may not engage in multiple transactions at the same time (ie,

[leetcode]Best Time to Buy and Sell Stock III @ Python

原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: 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 two transa

LeetCode&mdash;&mdash;Best Time to Buy and Sell Stock III (股票买卖时机问题3)

问题: 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 two transactions. 依旧还是那个熟悉的味道,如果II是I的另一个版本,那么III就是他们的一个升级版. 在这个版本中只能交易两次,也就是买卖各两次,其它条

leetcode&mdash;&mdash;Best Time to Buy and Sell Stock III

思路:将两段最大的利润相加,一段的最大利润不一定只在一个上升期内,例如在绿线间就有两个上升区间         只要算出i之前的最大利润和i之后的最大利润相加就行,因为i不可能同时是峰值或者谷值,所以能保证不是在同一天买进和卖出   绿线的利润加上红线利润是最大利润. class Solution { // 算出利润最大的两段 // 算出i之前最大的利润和i之后最大的利润 // l[]: i之前最大的利润 // r[]: i之后最大的利润 // minVal: i之前最小值 // maxVal: