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.
배열이 주어질때 주어진 규칙대로 숫자들을 가져와 합한 최대값을 구하라
Input: nums = [2,2,3,3,3,4]
Output: 9
Explanation: You can perform the following operations:
- 3을 가져오면 3 두개밖에 더 남지 않아 3 + 3 + 3 = 9 가 최대가 된다.
class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
maximum = max(nums)
if maximum == 0:
return 0
acc = [0] * (maximum + 1)
for num in nums:
acc[num] += num
dp = [0] * (maximum + 1)
dp[0] = 0
dp[1] = acc[1]
for i in range(2, maximum + 1):
dp[i] = max(
dp[i-2] + acc[i],
dp[i-1]
)
return dp[-1]