Delete and Earn
Updated
Problem statement
You are given an integer array nums. Repeatedly choose one remaining occurrence of a value x, add x to your score, and remove that occurrence. Also remove every remaining occurrence of x - 1 and x + 1 without scoring them.
Other occurrences of x remain available. Continue until the array is empty. Return the maximum total score obtainable over all choices.
For this exercise, assume the score for an empty input is zero.
Function
deleteAndEarn(nums: int[]) → intExamples
Example 1
nums = [8,3,4,2,2,7]return = 16Choose both occurrences of 2, then 4 and 8, earning 2 + 2 + 4 + 8 = 16. Choosing 2 removes 3; choosing 8 removes 7. These choices attain the maximum.
Example 2
nums = [2,3]return = 3Choosing 2 first earns only 2; choosing 3 first earns 3. The score depends on the choice, and the maximum is 3.
Constraints
- For this exercise, assume 0 <= nums.length <= 20000.
- For this exercise, assume 1 <= nums[i] <= 10000.
More Morgan Stanley Online Assessment questions
- Three-Resource 0/1 KnapsackOA · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
- Count Shipment SchedulesOA · Seen Apr 2026
- Interleave Array HalvesOA · Seen Apr 2026
- Get Patch SequenceOA · Seen Feb 2025
- String Subsequences 🐳OA · Seen Mar 2024
- Binary Tree Level OrderOA · Seen Feb 2024
- Merge IntervalsOA · Seen Feb 2024
- Format Chemical SymbolsOA · Seen Feb 2023
Similar questions at other companies
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Maximum Subarray SumStraive · OA
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
- Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions
- API Bandwidth AllocationArista Networks · OAArista Networks interview questions
- Maximum Points by Deleting ElementsIBM · OAIBM interview questions
- House RobberWalmart · OAWalmart interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
- Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)