nathanwailes

LeetCode 121 - Best Time to Buy and Sell Stock - 2023.10.12 solution

Oct 11th, 2023
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. class Solution:
  2.     def maxProfit(self, prices: List[int]) -> int:
  3.         """
  4.        """
  5.         buy_index = 0
  6.         sell_index = 1
  7.  
  8.         max_profit = 0
  9.  
  10.         while sell_index < len(prices):
  11.             current_profit = prices[sell_index] - prices[buy_index]
  12.             max_profit = max(current_profit, max_profit)
  13.  
  14.             if prices[sell_index] < prices[buy_index]:
  15.                 buy_index = sell_index
  16.            
  17.             sell_index += 1
  18.        
  19.         return max_profit
Advertisement
Add Comment
Please, Sign In to add comment