**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 II** **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 II– LeetCode Problem

Best Time to Buy and Sell Stock II– LeetCode Problem

**Problem:**

You are given an integer array `prices`

where `prices[i]`

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

day.^{th}

On each day, you may decide to buy and/or sell the stock. You can only hold **at most one** share of the stock at any time. However, you can buy it then immediately sell it on the **same day**.

Find and return *the maximum profit you can achieve*.

**Example 1:**

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

**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. Total profit is 4.

**Example 3:**

Input:prices = [7,6,4,3,1]Output:0Explanation:There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.

**Constraints:**

`1 <= prices.length <= 3 * 10`

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

^{4}

Best Time to Buy and Sell Stock II– LeetCode Solutions

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

class Solution { public: int maxProfit(vector<int>& prices) { int sell = 0; int hold = INT_MIN; for (const int price : prices) { sell = max(sell, hold + price); hold = max(hold, sell - price); } return sell; } };

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

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

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

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