Created
July 10, 2021 10:10
-
-
Save fpaupier/dec3fb095f9204d241e06aa638af6726 to your computer and use it in GitHub Desktop.
740. Delete and earn
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
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