Leetcode 740 Solution

This article provides solution to leetcode question 740 (delete-and-earn)

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)