๐Ÿ“ฆ Adib234 / Leetcode

๐Ÿ“„ best_time_to_buy_and_sell_stock.py ยท 17 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """

        maxprofit = 0
        tempmin = float('inf')

        for i in range(len(prices)):
            if tempmin > prices[i]:
                tempmin = prices[i]
            else:
                maxprofit = max(maxprofit, prices[i]-tempmin)
        return maxprofit