Created
October 25, 2023 22:28
-
-
Save doyedele1/245032ef7f97f34fc9023dbb846e6b42 to your computer and use it in GitHub Desktop.
740. Delete and Earn
This file contains hidden or 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
''' | |
TC: O(n) | |
SC: O(n) | |
''' | |
from typing import List | |
class Solution: | |
def deleteAndEarn(self, nums: List[int]) -> int: | |
arrNums = [0] * (max(nums) + 1) | |
for num in nums: | |
arrNums[num] += num | |
e1, e2 = 0, 0 | |
for num in arrNums: | |
currEarn = max(e2, num + e1) | |
e1 = e2 | |
e2 = currEarn | |
return e2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment