Data Structures & AlgorithmsVery hardLeetCode-style · Codeforces Div2 C

Each turn you pick a number x from [3, 4, 2, 4, 4, 6], earn x points, and then delete EVERY copy of x−1 and x+1. You may keep doing this until the array is empty. What is the maximum total points?

  1. A. 21
  2. B. 16
  3. C. 23
  4. D. 20

Correct answer: D. 20

Bucket the values: take[v] = v × count(v). Because taking v forbids v−1 and v+1, the choice is a house-robber DP over the values 1 to max. Greedily taking the most valuable bucket can block several cheaper ones.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic