best time to buy and sell stock
#array, #easy, #twopointers, link to problem ↗
thinking
Section titled “thinking”we will be using two pointers both going in the same direction. if the diff is negative that means we found the new lowest number so we directly skip to it.
solution
Section titled “solution”def maxProfit(self, prices: List[int]) -> int: l,r = 0,1 maxP = 0
while r < len(prices): if(prices[l] < prices[r]): profit = prices[r] - prices[l] maxP = max(maxP, profit) else: l=r r+=1
return maxP