Skip to content

Instantly share code, notes, and snippets.

@idfumg
Created July 27, 2024 10:40
Show Gist options
  • Save idfumg/9458a06bbdb4b324e7672cd5e530478d to your computer and use it in GitHub Desktop.
Save idfumg/9458a06bbdb4b324e7672cd5e530478d to your computer and use it in GitHub Desktop.
class Solution:
def maxProfit(self, nums: List[int]) -> int:
return sol8(nums)
def sol1(nums):
N = len(nums)
INF = 10**10
@cache
def run(idx, taken):
if idx == N and taken: return -INF
if idx == N: return 0
if taken: return max(run(idx + 1, 1), nums[idx])
return max(run(idx + 1, 0), -nums[idx] + run(idx + 1, 1))
return run(0, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment