8000 Create December-13.cpp · Hunterdii/Leetcode-POTD@32d112a · GitHub
[go: up one dir, main page]

Skip to content

Commit 32d112a

Browse files
authored
Create December-13.cpp
1 parent b1b5865 commit 32d112a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
long long findScore(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<pair<int, int>> indexedNums;
6+
7+
for (int i = 0; i < n; i++) {
8+
indexedNums.emplace_back(nums[i], i);
9+
}
10+
11+
sort(indexedNums.begin(), indexedNums.end());
12+
vector<bool> vis(n, false);
13+
long long ans = 0;
14+
15+
for (auto& [val, idx] : indexedNums) {
16+
if (!vis[idx]) {
17+
ans += val;
18+
vis[idx] = true;
19+
if (idx > 0) vis[idx - 1] = true;
20+
if (idx < n - 1) vis[idx + 1] = true;
21+
}
22+
}
23+
24+
return ans;
25+
}
26+
};

0 commit comments

Comments
 (0)
0