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 (ie, you must sell the stock before you buy again).

【题意】

给定一个数组prices, prices[i]表示第i天的股价。本题规定最多只能买卖两次,问最大收益是多少

【思路】

分别计算买卖一次的最大收益maxProfit1和买卖2次的最大收益maxProfit2,然后求最大值。

买卖一次的解法已经有了,详见Best Time to Buy and Sell Stock。

买卖两次的话我们需要确定转折点在什么地方。即在第i天卖出,在第i+1天买入。为了得到最大值我们需要知道我在第i天卖出的最大收益是多少,在第i+1天买入的最大收入是多少。 求每天卖出可获得的最大收益Best Time to Buy and Sell Stock中已经给出解法,只需要one-pass就完成。那么怎么计算每天买入可获得的最大收益呢?一样的,只不过换了一个方向而已。

为此我们维护两个数组buyProfit, sellProfit, sellProfit[i]表示在第i天卖出可以获得最大收益,buyProfit[i]表示在第i天买入可获得最大收入。则两次买卖的最大收益maxProfit2=max(buyProfit[i]+sellProfit[i+1]) i=1,2,3,....n-3,   其中n为prices数组的长度。

【代码】

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int size=prices.size();
        if(size<=1)return 0;

        int*back=new int[size];
        int*front=new int[size];
        int maxProfit=0;
        int minPrice=prices[0];
        int maxPrice=prices[size-1];
		back[size-1]=front[0]=0;
        // 求出以i结尾的前半段区间上买卖一次可获得最大收益
		maxProfit=0;
        for(int i=1; i<size; i++){
            int profit=prices[i]-minPrice;
			if(profit>maxProfit)maxProfit=profit;
            front[i]=maxProfit;
            if(prices[i]<minPrice)minPrice=prices[i];
        }
        // 求出以i开始的后半段区间上买卖一次可获得最大收益
		maxProfit=0;
        for(int i=size-2; i>=0; i--){
			int profit=maxPrice-prices[i];
			if(profit>maxProfit)maxProfit=profit;
            back[i]= maxProfit;
            if(prices[i]>maxPrice)maxPrice=prices[i];
        }

        //求两次买卖的最大值
		maxProfit=0;
        for(int i=0; i<size; i++){
            if(i==size-1){
				if(front[i]>maxProfit)maxProfit=front[i];
			}
			else{
				if(front[i]+back[i+1]>maxProfit)maxProfit=front[i]+back[i+1];
			}
        }

        return maxProfit;
    }
};

LeetCode: Best Time to Buy and Sell Stock III [123],布布扣,bubuko.com

时间: 2024-12-15 01:44:07

LeetCode: Best Time to Buy and Sell Stock III [123]的相关文章

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

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 动态规划

本题是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可以求得. 这里求

[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: