Premium Only Content
2593. Find Score of an Array After Marking All Elements
You are given an array nums consisting of positive integers.
Starting with score = 0, apply the following algorithm:
Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
Add the value of the chosen integer to score.
Mark the chosen element and its two adjacent elements if they exist.
Repeat until all the array elements are marked.
Return the score you get after applying the above algorithm.
Example 1:
Input: nums = [2,1,3,4,5,2]
Output: 7
Explanation: We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2].
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2].
- 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2].
Our score is 1 + 2 + 4 = 7.
Example 2:
Input: nums = [2,3,5,1,3,2]
Output: 5
Explanation: We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2].
- 2 is 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].
- 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2].
Our score is 1 + 2 + 2 = 5.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 106
#define ll long long
class Solution {
public:
long long findScore(vector<int>& nums) {
ll score = 0;
int n = nums.size();
vector<pair<int,int>> vec;
unordered_map<int,int> mp;
for(int i=0; i<n; i++)
vec.push_back({nums[i], i});
sort(vec.begin(), vec.end());
for(int i=0; i<n; i++){
if(mp.find(vec[i].first) == mp.end() && nums[vec[i].second] != INT_MAX){
if(vec[i].second-1 >= 0) nums[vec[i].second - 1] = INT_MAX;
if(vec[i].second+1 < n) nums[vec[i].second + 1] = INT_MAX;
score += vec[i].first;
}
}
return score;
}
};
-
LIVE
Mally_Mouse
1 hour agoLet's Play!! -- Jackbox: Trivia Murder Party!!
479 watching -
1:24:33
Savanah Hernandez
4 hours agoThe Culture Has Shifted & Accountability Is Coming
23.1K10 -
1:09:10
PMG
1 day ago $7.74 earned"HHS Whistleblower Speaks Out After 300,000 Migrant Children Go Missing"
51.3K9 -
1:40:48
The Quartering
5 hours agoMystery Drones Spraying Chemicals, Firing Bullets & Everyone's Lying!
96.9K38 -
2:59:59
vivafrei
21 hours agoConversation with a Lefty: "Pastor Ben" Talking Daniel Perry, MAGA & Much More! Viva Frei Live
102K99 -
12:21
Silver Dragons
6 hours agoSilver Price Pushed Down - Is There Any Hope in 2025?
42.2K4 -
LIVE
tacetmort3m
16 hours ago🔴 LIVE - THIS GAME IS ABSOLUTE CINEMA - INDIANA JONES AND THE GREAT CIRCLE - PART 3
222 watching -
1:58:49
The Charlie Kirk Show
6 hours agoThe New Jersey Drone Mystery + "Only" 26 J6 Informants + Pop Culture Power Hour | Kelly | 12.13.24
146K71 -
29:49
Brewzle
6 hours agoThis Distiller Is The Mad Scientist Of Corn
27K4 -
17:53
Misha Petrov
19 hours agoWoman Sleeps with 100 Men in One Day, Plans 1,000 Next?! The Sad Reality of OF Culture
78.2K103