Advertisement
smj007

Untitled

Aug 3rd, 2023
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. class Solution:
  2.     def maxProfit(self, prices: List[int]) -> int:
  3.  
  4.         """
  5.        2D DP: each row cell stores [min_price_so_far, max_profit_so_far]
  6.        """
  7.         dp = [[0, 0] for _ in range(len(prices))]
  8.         dp[0][0] = prices[0]
  9.  
  10.         for i in range(1, len(prices)):
  11.             dp[i][0] = min(dp[i-1][0], prices[i])
  12.             dp[i][1] = max(dp[i-1][1], prices[i] - dp[i][0])
  13.  
  14.         return dp[len(prices)-1][1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement