740. Delete and Earn

You are given an integer array nums. You want to maximize the number of points you get by performing the following operation any number of times:

  • Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums[i] - 1 and every element equal to nums[i] + 1. Return the maximum number of points you can earn by applying the above operation some number of times.
def deleteAndEarn(self, nums: List[int]) -> int: 
    m = max(nums) 
    cum = [0] * (m + 1) 
    for x in nums: 
        cum[x] += x 
        
    two_back, one_back = 0, cum[1] 
        
    for i in range(2, m + 1): 
        two_back, one_back = one_back, max(one_back, two_back + cum[i]) 
    return one_back