LeetCode 121 Best Time to Buy and Sell Stock

LeetCode 121 Best Time to Buy and Sell Stock Problem

Download Code
class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        length = len(prices)
        if length == 0:
            return 0
        max_profit, low = 0, prices[0]
        for i in range(1, length):
            if low > prices[i]:
                low = prices[i]
            else:
                temp = prices[i] - low
                if temp > max_profit:
                    max_profit = temp
        return max_profit
Download Best Time to Buy and Sell Stock.py

List of all Best Time to Buy and Sell Stock problems

Leetcode 121 Best Time to Buy and Sell Stock problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.