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:20:27
Glenn Greenwald
3 hours agoLee Fang Answers Your Questions on Charlie Kirk Assassination Fallout; Hate Speech Crackdowns, and More; Plus: "Why Superhuman AI Would Kill Us All" With Author Nate Soares | SYSTEM UPDATE #518
49.4K19 -
1:03:06
BonginoReport
4 hours agoLyin’ Jimmy Kimmel Faces The Music - Nightly Scroll w/ Hayley Caronia (Ep.137)
99.8K47 -
55:40
Donald Trump Jr.
8 hours agoThe Warrior Ethos & America's Mission, Interview with Harpoon Ventures Founder Larsen Jensen | Triggered Ep275
49.7K51 -
1:12:08
TheCrucible
4 hours agoThe Extravaganza! EP: 39 (9/18/25)
99.1K14 -
1:21:41
Kim Iversen
6 hours agoNick Fuentes Denies Israel Killed Charlie Kirk | Right-Wing CANCELS Jimmy Kimmel
44.3K202 -
1:01:59
Candace Show Podcast
4 hours agoEXCLUSIVE! Another Photo Of Tyler Robinson | Candace Ep 238
100K322 -
2:21:09
Redacted News
5 hours agoWhat are they hiding? New video evidence in Charlie Kirk's Shooting SHAKES FBI'S case | Redacted
160K318 -
41:53
Kimberly Guilfoyle
7 hours agoCharlie's Legacy and Our Mission
57.6K10 -
1:07:55
vivafrei
6 hours agoJimmy Kimmel Out Indefinitely! Trump "Srubs" Study on Right Wing Violence? Clinton Tweet & MORE
222K88 -
1:35:02
The Quartering
7 hours agoNuclear Fallout From Jimmy Kimmel Firing, New Head Of TP USA, Obama Whines
243K75