Leetcode 740 Solution
This article provides solution to leetcode question 740 (delete-and-earn)
Access this page by simply typing in "lcs 740" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/delete-and-earn
Solution
class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
m = collections.Counter(nums)
avoid = using = 0
for k in sorted(m):
if k - 1 in m:
avoid, using = max(using, avoid), avoid + m[k] * k
else:
avoid, using = max(using, avoid), max(using, avoid) + m[k] * k
return max(avoid, using)