leetcode——Best Time to Buy and Sell Stock III

思路:将两段最大的利润相加,一段的最大利润不一定只在一个上升期内,例如在绿线间就有两个上升区间

        只要算出i之前的最大利润和i之后的最大利润相加就行,因为i不可能同时是峰值或者谷值,所以能保证不是在同一天买进和卖出

 

绿线的利润加上红线利润是最大利润。

class Solution {
    // 算出利润最大的两段
    // 算出i之前最大的利润和i之后最大的利润
    // l[]: i之前最大的利润
    // r[]: i之后最大的利润
    // minVal: i之前最小值
    // maxVal: i之后最大值
public:
    int maxProfit(vector<int> &prices) {
        if(prices.empty()) return 0;
        const int size = prices.size();
        int l[size], r[size], minVal(prices[0]), maxVal(prices[size-1]);
        // 算出i之前的最大利润
        l[0] = 0;
        for(int i = 1; i < size; i ++){
            l[i] = max(l[i-1], prices[i] - minVal);
            minVal = min(prices[i], minVal);
        }
        // 算出i之后的最大利润
        r[size-1] = 0;
        for(int i = size-2; i >= 0; i --){
            r[i] = max(r[i+1], maxVal - prices[i]);
            maxVal = max(maxVal, prices[i]);
        }
        // 算出总的最大利润
        int profit(0);
        for(int i = 0; i < size; i ++){
            profit = max(profit, l[i] + r[i]);
        }
        return profit;

    }
};
时间: 2024-10-24 11:44:54

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

本题是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就是他们的一个升级版. 在这个版本中只能交易两次,也就是买卖各两次,其它条