Choose Values without Earning from Adjacent Values

Quick Overview

Choose Values without Earning from Adjacent Values. An operation selects one remaining occurrence x, deletes it and earns x points, then deletes all remaining occurrences of x-1 and x+1 without points.

Choose Values without Earning from Adjacent Values

Company: Walmart Labs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem An operation selects one remaining occurrence x, deletes it and earns x points, then deletes all remaining occurrences of x-1 and x+1 without points. Repeat as desired. Return the maximum total points. ```cpp class Solution { public: int deleteAndEarn(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [1,1,2,4,4] Output: 10 Explanation: Earn both ones and both fours: 2+8=10. ``` **Example 2:** ```text nums = [5,5,6] Output: 10 Explanation: Taking both fives earns 10, better than taking 6. ``` ### Constraints - Array length ranges from 1 to 20000. - Each entry is an integer in [1,10000]. ### Contract considerations Include every deletion side effect and permit any number of operations. ```hint Read the boundary condition Can equal values be compressed into one weighted choice? ``` ### Boundary reminders Include every deletion side effect and permit any number of operations.

Overview: Choose Values without Earning from Adjacent Values. An operation selects one remaining occurrence x, deletes it and earns x points, then deletes all remaining occurrences of x-1 and x+1 without points.

Walmart Labs logo
Walmart Labs
Sep 7, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

An operation selects one remaining occurrence x, deletes it and earns x points, then deletes all remaining occurrences of x-1 and x+1 without points. Repeat as desired. Return the maximum total points.

class Solution {
public:
    int deleteAndEarn(vector<int>& nums);
};

Examples

Example 1:

nums = [1,1,2,4,4]
Output: 10
Explanation: Earn both ones and both fours: 2+8=10.

Example 2:

nums = [5,5,6]
Output: 10
Explanation: Taking both fives earns 10, better than taking 6.

Constraints

  • Array length ranges from 1 to 20000.
  • Each entry is an integer in [1,10000].

Contract considerations

Include every deletion side effect and permit any number of operations.

Boundary reminders

Include every deletion side effect and permit any number of operations.