Created
July 27, 2024 10:40
-
-
Save idfumg/9458a06bbdb4b324e7672cd5e530478d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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