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;
}
};
-
19:19
World2Briggs
1 day ago $0.59 earnedTop 10 Amazing States With The Lowest Taxes.
4.26K -
LIVE
BEK TV
22 hours agoTrent Loos in the Morning - 9/16/2025
345 watching -
LIVE
The Bubba Army
21 hours agoDid Charlie Kirk's Killer, Confess On Discord? - Bubba the Love Sponge® Show | 9/16/25
2,813 watching -
8:41
Zoufry
1 day agoInside The Cartels Training Camps for Assassins
5.17K1 -
12:37
itsSeanDaniel
1 day agoFake Republican REFUSES to Stand Up for Charlie Kirk
12.1K22 -
1:01:55
MTNTOUGH Podcast w/ Dustin Diefenderfer
21 hours agoJason Khalipa: American Men: Get Off the Couch and Rise Up Now | MTNPOD #133
20.4K44 -
5:17
Blackstone Griddles
12 hours agoEasy Weeknight Meals: Creamy Parmesan Beef Pasta
11.7K4 -
7:08
Rena Malik, M.D.
22 hours ago $1.02 earnedFour totally surprising causes of Back Pain
10.7K1 -
6:27
DropItLikeItsScott
14 hours ago $0.77 earnedIs This the Coolest Gun Accessory Ever? The Gun Guardian
12.2K -
19:05
Michael Feyrer Jr
23 hours agoCan you even fit this much FAIL in one video? $10K Challenge Week 1
9.87K