712. Take and Burn
At a bonfire festival you hold a pile of logs, nums, each stamped with a positive number. In one round you pick a stamp value v, burn every log stamped v, and score v points for each of them. The flames scorch the neighbours: all logs stamped v - 1 or v + 1 crumble and can never be used afterwards.
Rounds continue for as long as you like. Return the largest total score you can collect. The pile is never empty.
Aim for time O(n + m) and space O(m), where m is the largest stamp.
Example 1
- Input:
- nums = [12,13,13,15,16,16,16]
- Output:
- 74
- Explanation:
Burning all 13s (26 points) and all 16s (48 points) scores 74, because 15 touches 16 and 12 touches 13.
Example 2
- Input:
- nums = [4,10,4,10,6,6,6]
- Output:
- 46
- Explanation:
No two stamps differ by exactly 1, so every value can be burned: 8 + 18 + 20 = 46.
Constraints
1 ≤ nums.length ≤ 105
1 ≤ nums[i] ≤ 104
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n + m)
- Space
- O(m)