Input: nums =[2,1,3,4,5,2]Output: 7Explanation: We mark the elements as follows:-1is the smallest unmarked element, so we mark it and its two adjacent elements:[2̲ ,1̲ ,3̲ ,4,5,2].-2is the smallest unmarked element, so we mark it and its left adjacent element:[2̲ ,1̲ ,3̲ ,4,5̲ ,2̲].-4is the only remaining unmarked element, so we mark it:[2̲ ,1̲ ,3̲ ,4̲ ,5̲ ,2̲].Our score is1+2+4=7.
Input: nums =[2,3,5,1,3,2]Output: 5Explanation: We mark the elements as follows:-1is the smallest unmarked element, so we mark it and its two adjacent elements:[2,3,5̲ ,1̲ ,3̲ ,2].-2is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element:[2̲ ,3̲ ,5̲ ,1̲ ,3̲ ,2].-2is the only remaining unmarked element, so we mark it:[2̲ ,3̲ ,5̲ ,1̲ ,3̲ ,2̲].Our score is1+2+2=5.
classSolution {
publiclongfindScore(int[] nums) {
int n = nums.length;
boolean[] marked =newboolean[n];
long score = 0;
while (true) {
int minVal = Integer.MAX_VALUE;
int minIndex =-1;
for (int i = 0; i < n; i++) {
if (!marked[i]&& nums[i]< minVal) {
minVal = nums[i];
minIndex = i;
}
}
if (minIndex ==-1) break; // All elements are marked// Add the minimum value to score score += nums[minIndex];
// Mark the chosen element and its adjacent elementsif (minIndex > 0) marked[minIndex - 1]=true;
marked[minIndex]=true;
if (minIndex < n - 1) marked[minIndex + 1]=true;
}
return score;
}
}
classSolution:
deffind_score(self, nums: List[int]) -> int:
n: int = len(nums)
marked: List[bool] = [False] * n
score: int =0whileTrue:
min_val: int = float('inf')
min_index: int =-1for i in range(n):
ifnot marked[i] and nums[i] < min_val:
min_val = nums[i]
min_index = i
if min_index ==-1:
break# All elements are marked# Add the minimum value to score score += nums[min_index]
# Mark the chosen element and its adjacent elementsif min_index >0:
marked[min_index -1] =True marked[min_index] =Trueif min_index < n -1:
marked[min_index +1] =Truereturn score
The key observation for an efficient solution is that we need to repeatedly find the smallest unmarked element and mark it along with its adjacent elements. Using a min-heap (priority queue) allows us to efficiently retrieve the smallest element in logarithmic time. By marking adjacent elements immediately upon processing the smallest element, we ensure that these elements are not reconsidered in future iterations, preserving the constraint of marking and managing all elements efficiently.
Here is the approach:
Initial Setup:
Create a priority queue (min-heap) and insert each element of the array along with its index into the heap.
Maintain a boolean array to keep track of which elements are marked.
Processing Elements:
Continuously extract the smallest element from the heap.
If the extracted element is already marked, skip it.
If it is unmarked, add its value to the score and mark it.
Additionally, mark its adjacent elements (if they exist).
Termination:
The process stops when all elements of the array are either extracted from the heap or marked.