Untitled
unknown
plain_text
9 months ago
1.0 kB
4
Indexable
import java.util.PriorityQueue;
public class Solution {
public int findScore(int[] nums) {
int n = nums.length;
PriorityQueue<Integer> minHeap = new PriorityQueue<>((a, b) -> nums[a] == nums[b] ? a - b : nums[a] - nums[b]);
// Add all indices to the heap
for (int i = 0; i < n; i++) {
minHeap.add(i);
}
boolean[] marked = new boolean[n];
int score = 0;
while (!minHeap.isEmpty()) {
int index = minHeap.poll();
// Skip if already marked
if (marked[index]) continue;
// Add the value to the score
score += nums[index];
// Mark the chosen element and its adjacent elements
marked[index] = true;
if (index > 0) marked[index - 1] = true;
if (index < n - 1) marked[index + 1] = true;
}
return score;
}
}
Editor is loading...
Leave a Comment