Find Score of an Array After Marking All Elements - Problem

You're given an array of positive integers, and your mission is to accumulate the highest possible score using a specific marking strategy!

The Algorithm:

  1. Start with score = 0
  2. Find the smallest unmarked element in the array (if there's a tie, pick the one with the smallest index)
  3. Add this element's value to your score
  4. Mark this element AND its immediate neighbors (left and right adjacent elements, if they exist)
  5. Repeat until every element is marked

Your goal is to return the final score after applying this greedy marking algorithm.

Example: For array [2,1,3,4,5,2], you'd first pick 1 (smallest), mark positions 0,1,2, then pick 2 from position 5, and so on.

Input & Output

example_1.py โ€” Basic Array
$ Input: [2,1,3,4,5,2]
โ€บ Output: 7
๐Ÿ’ก Note: Step 1: Pick 1 (smallest), score=1, mark indices [0,1,2]. Step 2: Pick 2 from index 5 (smallest remaining), score=3, mark indices [4,5]. Step 3: Pick 4 from index 3 (only remaining), score=7. Final score = 7.
example_2.py โ€” Small Array
$ Input: [2,3,5,1,3,2]
โ€บ Output: 5
๐Ÿ’ก Note: Step 1: Pick 1 (index 3), score=1, mark [2,3,4]. Step 2: Pick 2 (index 0), score=3, mark [0,1]. Step 3: Pick 2 (index 5), score=5, mark [5]. All elements marked, final score = 5.
example_3.py โ€” Single Element
$ Input: [1]
โ€บ Output: 1
๐Ÿ’ก Note: Only one element exists. Pick 1, add to score. No neighbors to mark. Final score = 1.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 106
  • All elements are positive integers
  • Array can contain duplicate values

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ Treasure Hunter's Cave AlgorithmMagical Cave with Gems๐Ÿ’Ž2pos 0๐Ÿ’Ž1pos 1SMALLEST!๐Ÿ’Ž3pos 2๐Ÿ’Ž4pos 3๐Ÿ’Ž5pos 4๐Ÿ’Ž2pos 5After Picking Gem 1:๐Ÿšซ2SEALEDโœ“1TAKEN๐Ÿšซ3SEALED๐Ÿ’Ž4NEXT MIN๐Ÿ’Ž5available๐Ÿ’Ž2SMALLEST!Score: 1๐ŸŽฏ Treasure Hunter's Strategy (Min-Heap)1. Priority Queue: Always know which gem is smallest without searching entire cave2. Greedy Choice: Pick smallest gem to leave bigger treasures for later rounds3. Sealing Magic: When you pick a gem, adjacent spots become inaccessibleโšก Time Complexity: O(n log n) vs O(nยฒ) for scanning entire cave each time๐Ÿ† Final treasure collected efficiently using heap-based priority system!
Understanding the Visualization
1
Survey Cave
Use a priority system (heap) to know which gem is smallest
2
Pick Smallest
Extract the smallest gem from your priority list
3
Seal Adjacent
Mark the gem's location and neighbors as sealed
4
Repeat Strategy
Continue until all gems are collected or sealed
Key Takeaway
๐ŸŽฏ Key Insight: Using a min-heap eliminates repeated scanning, reducing time from O(nยฒ) to O(n log n) while maintaining the greedy strategy of always picking the smallest available element.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
28.5K Views
High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen