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;
}
};
-
1:45:08
DDayCobra
8 hours ago $36.45 earnedDemocrats Caught LYING Again About Charlie Kirk's KILLER
63.6K79 -
19:23
DeVory Darkins
10 hours ago $11.88 earnedShocking Update Released Regarding Shooter's Roommate as Democrats Issue Insane Response
46.2K142 -
19:53
Stephen Gardner
11 hours ago🔥EXPOSED: Charlie Kirk Shooter's Trans Partner Tells FBI EVERYTHING!
58.3K316 -
2:47:25
BlackDiamondGunsandGear
7 hours agoAfter Hours Armory / RIP Charlie Kirk / What we know
38.6K5 -
29:09
Afshin Rattansi's Going Underground
1 day agoThe Political Life of Malcolm X: Busting the Myths (Prof. Kehinde Andrews)
49.9K13 -
2:47:25
DLDAfterDark
7 hours ago $4.60 earnedThe Assassination of Charlie Kirk - Just What We KNOW
27.7K6 -
1:33:56
MattMorseTV
8 hours ago $39.09 earned🔴Exposing his PARTNER IN CRIME.🔴
85K273 -
1:26:51
vivafrei
13 hours agoCharlie Kirk Assassination - When Peaceful Discussion Becomes Impossible - With Jose Vege
104K249 -
2:04:12
Mally_Mouse
1 day ago🌶️ 🥵Spicy BITE Saturday!! 🥵🌶️- Let's Play: Supermarket Together
51.3K3 -
1:15:37
BooniesHQ
12 hours ago $7.36 earnedGame Of SKATE Donny Hixson Vs. Chris Massie: Boonies Skate Night 2
69.4K7