Skip to content

Commit

Permalink
Create 2593_Find_Score_of_an_Array_After_Marking_All_Elements.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nothing-avil authored Dec 13, 2024
1 parent 2c7c09d commit 75c2e8b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 2593_Find_Score_of_an_Array_After_Marking_All_Elements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ███████╗ █████╗ ███╗ ██╗ ██████╗ █████╗ ██████╗ ██████╗ ██╗ ██╗
// ██╔════╝ ██╔══██╗ ████╗ ██║ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██║ ██║
// ███████╗ ███████║ ██╔██╗ ██║ ██║ ██║ ███████║ ██████╔╝ ██████╔╝ ███████║
// ╚════██║ ██╔══██║ ██║╚██╗██║ ██║ ██║ ██╔══██║ ██╔═██╗ ██╔══██╗ ██╔══██║
// ███████║ ██║ ██║ ██║ ╚████║ ██████╔╝ ██║ ██║ ██║ ██╗ ██████╔╝ ██║ ██║
// ╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
#pragma GCC optimize("Ofast", "inline", "ffast-math", "unroll-loops","no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native", "f16c")
auto init = []() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();
class Solution {
public:
long long findScore(vector<int>& nums) {
int n = nums.size();
vector<bool> vis(n+1);
long long score = 0;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
for(int i =0; i<n; i++){
pq.push({nums[i], i});
}
while(!pq.empty()){
int point = pq.top().first;
int idx = pq.top().second;
pq.pop();
score += point;
vis[idx] = true;
if(idx+1 < n){
vis[idx+1] = true;
}
if(idx-1 >= 0){
vis[idx-1] = true;
}
while(!pq.empty() && vis[pq.top().second]){
pq.pop();
}
}
return score;
}
};

0 comments on commit 75c2e8b

Please sign in to comment.