**LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++**

Hello **Programmers/Coders,** Today we are going to share ** solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python**. At Each Problem with Successful submission with

**all Test Cases Passed,**you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the **Best Time to Buy and Sell Stock III** **in C++, Java & Python-LeetCode problem**. We are providing the **correct and tested solutions** to coding problems present on **LeetCode**. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

**About LeetCode**

*LeetCode* is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.

LeetCode is for **software engineers who are looking to practice technical questions and advance their skills**. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either *Easy*, *Medium*, or *Hard*.

**LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:**

- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

** Link for the Problem** – Best Time to Buy and Sell Stock III– LeetCode Problem

Best Time to Buy and Sell Stock III– LeetCode Problem

**Problem:**

You are given an array `prices`

where `prices[i]`

is the price of a given stock on the `i`

day.^{th}

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:6Explanation: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:4Explanation: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:0Explanation:In this case, no transaction is done, i.e. max profit = 0.

**Constraints:**

`1 <= prices.length <= 10`

^{5}`0 <= prices[i] <= 10`

^{5}

Best Time to Buy and Sell Stock III– LeetCode Solutions

Best Time to Buy and Sell Stock III Solution in C++:

class Solution { public: int maxProfit(vector& prices) { int sellTwo = 0; int holdTwo = INT_MIN; int sellOne = 0; int holdOne = INT_MIN; for (const int price : prices) { sellTwo = max(sellTwo, holdTwo + price); holdTwo = max(holdTwo, sellOne - price); sellOne = max(sellOne, holdOne + price); holdOne = max(holdOne, -price); } return sellTwo; } };

Best Time to Buy and Sell Stock III Solution in Java:

class Solution { public int maxProfit(int[] prices) { int sellTwo = 0; int holdTwo = Integer.MIN_VALUE; int sellOne = 0; int holdOne = Integer.MIN_VALUE; for (final int price : prices) { sellTwo = Math.max(sellTwo, holdTwo + price); holdTwo = Math.max(holdTwo, sellOne - price); sellOne = Math.max(sellOne, holdOne + price); holdOne = Math.max(holdOne, -price); } return sellTwo; } }

Best Time to Buy and Sell Stock III Solution in Python:

class Solution: def maxProfit(self, prices: List[int]) -> int: sellTwo = 0 holdTwo = -inf sellOne = 0 holdOne = -inf for price in prices: sellTwo = max(sellTwo, holdTwo + price) holdTwo = max(holdTwo, sellOne - price) sellOne = max(sellOne, holdOne + price) holdOne = max(holdOne, -price) return sellTwo