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;
}
};
-
3:55:11
I_Came_With_Fire_Podcast
14 hours agoTrump SABOTAGE, LA FIRE CHIEF SUED, and BIDEN’S LAST F-U!
15.1K7 -
2:59:47
Joker Effect
5 hours agoUkraine in a video game? Hardest thing I have done. S.T.A.L.K.E.R.2 Heart of Chornobyl,
69.9K4 -
1:15:22
Flyover Conservatives
1 day agoEczema, Brain Fog, B.O., and Gas… Eating Steak and Butter Creates Ultimate Health Hack - Bella, Steak and Butter Gal | FOC Show
54.7K2 -
51:58
PMG
9 hours ago $2.41 earned"Can the Government Learn from Elon Musk’s 70% Labor Cut? A Deep Dive into Inefficient Agencies"
31.7K -
6:39:15
Amish Zaku
8 hours agoRumble Spartans #10 - New Year New Maps
28.9K2 -
1:04:58
In The Litter Box w/ Jewels & Catturd
1 day agoNo Tax On Tips! | In the Litter Box w/ Jewels & Catturd – Ep. 722 – 1/17/2025
152K32 -
5:35:39
Dr Disrespect
15 hours ago🔴LIVE - DR DISRESPECT - WARZONE - CRAZY CHALLENGES
170K33 -
1:16:30
Edge of Wonder
11 hours agoLA Fire Updates: Miracles Amidst the Devastation
46.8K14 -
54:54
LFA TV
15 hours agoBanning Mystery of the Ages | TRUMPET DAILY 1.17.25 7pm
37.9K8 -
1:47:13
2 MIKES LIVE
8 hours ago2 MIKES LIVE #168 Open Mike Friday!
32.5K3