Maximize points from deleting values when each pick removes all neighbors v-1 and v+1

Quick Overview

Pick elements from an integer array to earn their values as points, where each pick deletes every element one above or one below it, and find the largest total. It tests turning the problem into dynamic programming over distinct values and handling gaps between them.

Maximize points from deleting values when each pick removes all neighbors v-1 and v+1

Company: Akuna Capital

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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. ### Function Signature ```python def delete_and_earn(nums: list[int]) -> int: ``` ### Rules - Each operation earns points only for the one element you pick. The elements equal to `nums[i] - 1` and `nums[i] + 1` that it also deletes earn nothing. - Other elements equal to `nums[i]` are not deleted by the operation and can be picked in later operations. - You may stop at any time, including before the first operation. ### Constraints - `1 <= nums.length <= 2 * 10^4` - `1 <= nums[i] <= 10^4` ### Examples ```text Input: nums = [3,4,2] Output: 6 ``` Take 4 to earn 4 points, which deletes the 3. Then take 2 to earn 2 points, for a total of 6. Taking the 3 instead would delete both the 2 and the 4 and earn only 3. ```text Input: nums = [2,2,3,3,3,4] Output: 9 ``` Take a 3, which deletes both 2s and the 4, then take the other two 3s, for a total of 9. Taking the 2s and the 4 instead earns 2 + 2 + 4 = 8. ```text Input: nums = [1,3,3,5,6,6] Output: 19 ``` The values 1 and 3 do not conflict because 2 is absent, so take the 1 and both 3s for 7 points. Taking both 6s deletes the 5 and adds 12, for a total of 19.

Overview: Pick elements from an integer array to earn their values as points, where each pick deletes every element one above or one below it, and find the largest total. It tests turning the problem into dynamic programming over distinct values and handling gaps between them.

Akuna Capital logo
Akuna Capital
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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.

Function Signature

def delete_and_earn(nums: list[int]) -> int:

Rules

  • Each operation earns points only for the one element you pick. The elements equal to nums[i] - 1 and nums[i] + 1 that it also deletes earn nothing.
  • Other elements equal to nums[i] are not deleted by the operation and can be picked in later operations.
  • You may stop at any time, including before the first operation.

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • 1 <= nums[i] <= 10^4

Examples

Input: nums = [3,4,2]
Output: 6

Take 4 to earn 4 points, which deletes the 3. Then take 2 to earn 2 points, for a total of 6. Taking the 3 instead would delete both the 2 and the 4 and earn only 3.

Input: nums = [2,2,3,3,3,4]
Output: 9

Take a 3, which deletes both 2s and the 4, then take the other two 3s, for a total of 9. Taking the 2s and the 4 instead earns 2 + 2 + 4 = 8.

Input: nums = [1,3,3,5,6,6]
Output: 19

The values 1 and 3 do not conflict because 2 is absent, so take the 1 and both 3s for 7 points. Taking both 6s deletes the 5 and adds 12, for a total of 19.