Leetcode Problem 2593. Find Score of an Array After Marking All Elements

2593. Find Score of an Array After Marking All Elements

Leetcode Solutions

Priority Queue with Visited Array

  1. Initialize a priority queue (min heap) to store pairs of (value, index) from the input array nums.
  2. Initialize a visited array of the same length as nums with all elements set to False.
  3. Push all elements of nums along with their indices into the priority queue.
  4. Initialize score to 0.
  5. While the priority queue is not empty: a. Pop the top element (smallest unmarked element) from the priority queue. b. If the element at the popped index is unmarked (visited array is False at that index): i. Add the value to score. ii. Mark the element and its adjacent elements as visited.
  6. Return the score.
UML Thumbnail

HashMap with Visited Array

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...