Day 9 LeetCode
👨‍💻

Day 9 LeetCode

Tags
Data Structures
Python
Java
Computer Science
Published
Jan 24, 2021

Day 9 Task:

→ 121. Best Time to Buy and Sell Stock:
 

Solutions:

Please review all the solutions and try implementing on your own if you haven’t gotten it.
 
class Solution(object):
    def maxProfit(self, prices):
        '''
        Solution 1, we initialize the buy price to first day, find the minimum buy from 2nd day
        subtract the prices[i]-buy to get profit, and check the greater profit.
        '''
        buy= prices[0]
        profit = 0
        
        n = len(prices)
        for i in range(1,n):
            buy = min(buy,prices[i])
            profit = max(prices[i]-buy,profit)
        
        return profit