Skip to content

Instantly share code, notes, and snippets.

@fpaupier
Created July 10, 2021 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpaupier/dec3fb095f9204d241e06aa638af6726 to your computer and use it in GitHub Desktop.
Save fpaupier/dec3fb095f9204d241e06aa638af6726 to your computer and use it in GitHub Desktop.
740. Delete and earn
import collections
class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
m: int = max(nums)
buckets: List[int] = [0 for _ in range(m+1)]
counter = collections.Counter(nums)
for k, v in counter.items():
buckets[k] = v*k
take, skip = 0, 0
for i in range(m+1):
take_i = skip + buckets[i]
skip_i = max(skip, take)
skip = skip_i
take = take_i
return max(take, skip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment