Skip to content

Commit 0748a38

Browse files
committed
Add C++ solution of problem #2593
1 parent bb212de commit 0748a38

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

2593/solution.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
long long findScore(vector<int>& A) {
4+
using Pair = std::pair<int, int>; // {num, idx}
5+
6+
int n = A.size();
7+
priority_queue<Pair, vector<Pair>, std::greater<Pair>> q;
8+
for(int i = 0; i < n; i++)
9+
q.push({A[i], i});
10+
11+
long long ans = 0LL;
12+
unordered_set<int> m; // marked indices
13+
while(!q.empty()) {
14+
auto [v, idx] = q.top();
15+
q.pop();
16+
printf("%d %d\n", v, idx);
17+
if(m.count(idx))
18+
continue;
19+
20+
ans += v;
21+
m.insert(idx);
22+
if(idx - 1 >= 0)
23+
m.insert(idx - 1);
24+
if(idx + 1 < n)
25+
m.insert(idx + 1);
26+
}
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)