-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2593_Find_Score_of_an_Array_After_Marking_All_Elements.cpp
- Loading branch information
1 parent
2c7c09d
commit 75c2e8b
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
2593_Find_Score_of_an_Array_After_Marking_All_Elements.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |