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;
}
};
-
5:28:29
Akademiks
6 hours agoDay 2/30. Smurk on Stream?? 50 cent keep going at Big Meech. Kendrick to Buy Kanye West Catalog?
65.9K2 -
1:35:28
BlackDiamondGunsandGear
10 hours agoThe TRUMP SLUMP?
28.4K6 -
3:13:59
SilverFox
4 hours ago🔴LIVE - OBLIVION IS BETTER THAN SKYRIM NOW
24.7K1 -
LIVE
SpartakusLIVE
8 hours agoDuos w/ StevieT || Trios or Quads Later?!
155 watching -
7:19:12
OhHiMark1776
9 hours ago🟢04-27-25 ||||| Halo Multiplayer Rumble: No. 13 ||||| Halo MCC (2019)
75.1K -
2:12:28
TheSaltyCracker
6 hours agoThey Killed Her ReeEEEe Stream 04-27-25
144K288 -
2:33:51
vivafrei
17 hours agoEp. 261: Criminal Judges ARRESTED! Election in Canada! Santos Sentenced! RFK Jr. & Autism & MORE!
187K129 -
6:24:31
Amish Zaku
9 hours agoRumble Spartans "The One Year" Event
46.3K1 -
LIVE
Illyes Jr Gaming
7 hours agoLaid Back Sunday Night Warzone Stream!
60 watching -
1:51:18
Nerdrotic
9 hours ago $6.91 earnedDiscoveries From Graham Hancock's "Fight for the Past" | Forbidden Frontier 099
48.4K12