Algorithm


Problem Nmae: 123. Best Time to Buy and Sell Stock III

You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete at most two transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

 

Example 1:

Input: prices = [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.

Example 2:

Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

 

Constraints:

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 105

Code Examples

#1 Code Example with C Programming

Code - C Programming


int maxProfit(int* prices, int pricesSize) {
    int buy1, buy2;     // max profit if I buy
    int sell1, sell2;   // max profit if I sell
    int i;
    
    buy1 = buy2 = 0x80000000;   // min of integer
    sell1 = sell2 = 0;
    
    for (i = 0; i  <  pricesSize; i ++) {
                        // possible profit
        buy1  = buy1  > 0     - prices[i] ? buy1  : 0     - prices[i];
        sell1 = sell1 > buy1  + prices[i] ? sell1 : buy1  + prices[i];
        buy2  = buy2  > sell1 - prices[i] ? buy2  : sell1 - prices[i];
        sell2 = sell2 > buy2  + prices[i] ? sell2 : buy2  + prices[i];
    }
    
    return sell2;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
prices = [3,3,5,0,0,3,1,4]

Output

x
+
cmd
6

#2 Code Example with Javascript Programming

Code - Javascript Programming


const maxProfit = function(prices) {
  let maxProfit1 = 0
  let maxProfit2 = 0
  let lowestBuyPrice1 = Number.MAX_VALUE
  let lowestBuyPrice2 = Number.MAX_VALUE

  for (let p of prices) {
    maxProfit2 = Math.max(maxProfit2, p - lowestBuyPrice2)
    lowestBuyPrice2 = Math.min(lowestBuyPrice2, p - maxProfit1)
    maxProfit1 = Math.max(maxProfit1, p - lowestBuyPrice1)
    lowestBuyPrice1 = Math.min(lowestBuyPrice1, p)
  }
  return maxProfit2
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
prices = [3,3,5,0,0,3,1,4]

Output

x
+
cmd
6

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def maxProfit(self, prices):
        s1 = s2 = 0
        b1 = b2 = -float("inf")
        for p in prices:
            if -p > b1: b1 = -p
            if b1 + p > s1: s1 = b1 + p
            if s1 - p > b2: b2 = s1 - p
            if b2 + p > s2: s2 = b2 + p
        return s2
Copy The Code & Try With Live Editor

Input

x
+
cmd
prices = [1,2,3,4,5]

Output

x
+
cmd
4

#4 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _0123_BestTimeToBuyAndSellStock3
    {
        public int MaxProfit(int[] prices)
        {
            int buy1 = int.MaxValue, buy2 = int.MaxValue;
            int sell1 = 0, sell2 = 0;

            for (int i = 0; i  <  prices.Length; i++)
            {
                buy1 = Math.Min(buy1, prices[i]);
                sell1 = Math.Max(sell1, prices[i] - buy1);
                buy2 = Math.Min(buy2, prices[i] - sell1);
                sell2 = Math.Max(sell2, prices[i] - buy2);
            }

            return sell2;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
prices = [1,2,3,4,5]
Advertisements

Demonstration


Previous
#122 Leetcode Best Time to Buy and Sell Stock II Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#124 Leetcode Binary Tree Maximum Path Sum Solution in C, C++, Java, JavaScript, Python, C# Leetcode