Skip to content

Instantly share code, notes, and snippets.

@doyedele1
Created October 25, 2023 22:28
Show Gist options
  • Save doyedele1/245032ef7f97f34fc9023dbb846e6b42 to your computer and use it in GitHub Desktop.
Save doyedele1/245032ef7f97f34fc9023dbb846e6b42 to your computer and use it in GitHub Desktop.
740. Delete and Earn
'''
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