Ctrl K

Points Without Neighbors

Medium
Cards with numbers lie on a table, given as the array values. In one move you take any card and score as many points as it shows. Every card whose value is exactly one less or one more than the card you took then burns and can no longer score. You may make as many moves as you like. Return the largest score you can get.
Example 1
values=[4, 10, 5, 4] → 18

Take both 4s (the 5 burns) and the 10: 4 + 4 + 10 = 18. Starting with the 5 would burn both 4s and leave just 15.

Example 2
values=[1, 1, 2, 3, 3, 3] → 11

It pays to give up the 2: the 1s give 2 points and the 3s give 9, 11 in total.

Constraints
  • 1 ≤ len(values) ≤ 2·10⁴
  • 1 ≤ values[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit Into Dictionary Words MediumWords Hidden in a Text MediumTrees Built From Products MediumLongest Sum Chain MediumLongest Equal-Gap Chain Medium
loading Python…
Input
values=[4, 10, 5, 4]
Expected
18

Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.