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

解题思路

建立两个数组leftright,分别存储某个元素左边和右边所能获得的最大收益。即left[i]存储从[0, i]范围的最大收益;right[i]存储从[i, len - 1]范围的最大收益。

实现代码

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/22 18:08
    *  @Status   : Accepted
    *  @Runtime  : 16 ms
******************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int len = prices.size();
        if (len == 0)
        {
            return 0;
        }
        vector<int> left(len, 0);
        vector<int> right(len, 0);

        int i = 0;
        int low = prices[0];
        int profit = 0;
        while (i < len - 1)
        {
            if (prices[i] < low)
            {
                low = prices[i];
            }
            else if (prices[i] >= prices[i + 1])
            {
                profit = max(profit, prices[i] - low);
            }
            left[i] = profit;
            i++;
        }
        profit = max(profit, prices[i] - low);
        left[i] = profit;

        int high = prices[i];
        profit = 0;
        while(i > 0)
        {
            if (prices[i] > high)
            {
                high = prices[i];
            }
            else if (prices[i] <= prices[i - 1])
            {
                profit = max(profit, high - prices[i]);
            }
            right[i] = profit;
            i--;
        }
        profit = max(profit, high - prices[i]);
        right[i] = profit;

        i = 0;
        int max_profit = 0;
        while (i < len)
        {
            max_profit = max(max_profit, left[i] + right[i]);
            i++;
        }

        return max_profit;
    }
};

int main()
{
    int num[] = {1,2,3,4,5,6};
    vector<int> n(num, num + sizeof(num)/sizeof(int));

    Solution s;
    int profit = s.maxProfit(n);
    cout<<profit<<endl;
}

另一种解题思路可参考Best Time to Buy and Sell Stock IV

时间: 2024-09-15 18:28:28

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

[LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee. You may complete as many transactions as you like, but you need to pay the t

[LeetCode] 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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

[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 you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 解题思路

[LeetCode] 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 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, y

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,

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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

[LeetCode]--121. 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 you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Exam

【LeetCode从零单排】No121 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 you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 又

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers