LeetCode #122 Best Time to Buy and Sell Stock II Solution & Explanation

LeetCode Problem

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

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.

LeetCode#122 Examples
LeetCode#122 Constraints

Solution

Maybe we can’t find the best time to buy the stock, but we know which stock that we shouldn’t buy

Elon Musk - Tesla and Twitter meme

This problem is one of the series of 【Buy and Sell Stock】 problems.

Unlike the first problem you can only buy and sell once – LeetCode #121 Best Time to Buy and Sell Stock

In this case, you can buy and sell multiple times.

In our first solution, we Buy The Dip and Sell The Tip

If the price is decreasing, we will wait for the bottom.

In the other case, if the price is increasing, we will wait for the top and sell it.

C# Solution

Solution1

public class Solution {
    public int MaxProfit(int[] prices) {
        int res = 0;
        int buy = 0;

        for(int i = 0; i<prices.Length; i++)
        {
            while(i+1<prices.Length && prices[i]>=prices[i+1])
            {
                i++;
            }

            buy = prices[i];

            while(i+1<prices.Length && prices[i]<prices[i+1])
            {
                i++; 
            }

            res += prices[i]-buy;
        }
        return res;
    }
}

Solution2

The second solution is more easy, just check if the next price is higher than now price or not.

If higher, than add the difference of them.

public class Solution {
    public int MaxProfit(int[] prices) {
        int res = 0;
        for(int i = 1; i<prices.Length; i++)
        {
            if(prices[i]>prices[i-1])
            {
                res += prices[i]-prices[i-1];
            }
        }
        return res;
    }
}

Java Solution

Solution1

class Solution {
    public int maxProfit(int[] prices) {
        int res = 0;
        int buy = 0;

        for(int i = 0; i<prices.length; i++)
        {
            while(i+1<prices.length && prices[i]>=prices[i+1])
            {
                i++;
            }

            buy = prices[i];

            while(i+1<prices.length && prices[i]<prices[i+1])
            {
                i++; 
            }

            res += prices[i]-buy;
        }
        return res;
    }
}

Solution2

class Solution {
    public int maxProfit(int[] prices) {
        int res = 0;
        for(int i = 1; i<prices.length; i++)
        {
            if(prices[i]>prices[i-1])
            {
                res += prices[i]-prices[i-1];
            }
        }
        return res;
    }
}

Python3 Solution

Solution1

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res = 0
        buy = 0

        i = 0
        while(i<len(prices)):    
        ###for i in range(0,len(prices)):
            while(i+1<len(prices) and prices[i]>=prices[i+1]):
                i+=1
            
            print(i)
            buy = prices[i]

            while(i+1<len(prices) and prices[i]<prices[i+1]):
                i+=1
            
            res += (prices[i]-buy)
            i+=1
        
        return res

Solution2

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res = 0
        for i in range(1,len(prices)):
            if(prices[i]>prices[i-1]):
                res += prices[i]-prices[i-1]
        return res

JavaScript Solution

Solution1

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
    var res = 0;
    var buy = 0;

    for(let i = 0; i<prices.length; i++)
    {
        while(i+1<prices.length && prices[i]>=prices[i+1])
        {
            i++;
        }

        buy = prices[i];

        while(i+1<prices.length && prices[i]<prices[i+1])
        {
            i++; 
        }

        res += prices[i]-buy;
    }
    return res;
};

Solution2

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
    var res = 0;
    for(let i = 1; i<prices.length; i++)
    {
        if(prices[i]>prices[i-1])
        {
            res += prices[i]-prices[i-1];
        }
    }
    return res;
};

Conclusion

🧡If my solution helps, that is my honor!

If you got any problem about the explanation, please feel free to let me know

The problem link : Best Time to Buy and Sell Stock II – LeetCode

Random post

Leave a Reply

Your email address will not be published. Required fields are marked *