Skip to the code
cf.
+25 XP

#498 Collect Tag Points Without Neighbors

medium

A tagging game shows a list of numbered tags, tags. When you collect a value v, you earn v points for every tag with that value, but then you may not collect the values v - 1 or v + 1 at all.

Write tagPoints(tags) that returns the most points you can earn.

Example 1

Input: tags = [5,6,4]
Output: 10

Collect 6 and 4, which are not consecutive values: 6 + 4.

Example 2

Input: tags = [3,3,4,4,4,5]
Output: 12

Collect every 4: 4 + 4 + 4. Taking the 3s and the 5 instead gives only 11.

Constraints

  • 1 <= tags.length <= 20000
  • 1 <= tags[i] <= 10000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).