Advertisement
smj007

Untitled

Aug 3rd, 2023
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. class Solution:
  2.     def maxProfit(self, prices: List[int]) -> int:
  3.  
  4.         """
  5.        max_current_profit is either obtained by immediately adding current_profit at current index (2) to max_current_profit
  6.        max_current_profit is either obtained by current profit
  7.        """
  8.         max_current_profit = 0
  9.         max_profit_so_far = 0
  10.  
  11.         for i in range(1, len(prices)):
  12.             max_current_profit = max(
  13.                 prices[i] - prices[i-1],
  14.                 max_current_profit + (prices[i] - prices[i-1])
  15.             )
  16.             max_profit_so_far = max(max_profit_so_far, max_current_profit)
  17.  
  18.         return max_profit_so_far
  19.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement