Skip to content

Commit c89f8be

Browse files
committed
feat(leetcode): 2593 Find Score of an Array After Marking All Elements
1 parent 2fa8aa8 commit c89f8be

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## [2593. Find Score of an Array After Marking All Elements](https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/)
2+
<p>You are given an array <code>nums</code> consisting of positive integers.</p>
3+
4+
<p>Starting with <code>score = 0</code>, apply the following algorithm:</p>
5+
6+
<ul>
7+
<li>Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.</li>
8+
<li>Add the value of the chosen integer to <code>score</code>.</li>
9+
<li>Mark <strong>the chosen element and its two adjacent elements if they exist</strong>.</li>
10+
<li>Repeat until all the array elements are marked.</li>
11+
</ul>
12+
13+
<p>Return <em>the score you get after applying the above algorithm</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [2,1,3,4,5,2]
20+
<strong>Output:</strong> 7
21+
<strong>Explanation:</strong> We mark the elements as follows:
22+
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [<u>2</u>,<u>1</u>,<u>3</u>,4,5,2].
23+
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [<u>2</u>,<u>1</u>,<u>3</u>,4,<u>5</u>,<u>2</u>].
24+
- 4 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>1</u>,<u>3</u>,<u>4</u>,<u>5</u>,<u>2</u>].
25+
Our score is 1 + 2 + 4 = 7.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [2,3,5,1,3,2]
32+
<strong>Output:</strong> 5
33+
<strong>Explanation:</strong> We mark the elements as follows:
34+
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,<u>5</u>,<u>1</u>,<u>3</u>,2].
35+
- 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: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,2].
36+
- 2 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,<u>2</u>].
37+
Our score is 1 + 2 + 2 = 5.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
45+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
46+
</ul>
47+
48+
49+
## Hints
50+
1. Try simulating the process of marking the elements and their adjacent.
51+
2. If there is an element that was already marked, then you skip it.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/
4+
* Runtime: 47ms (98.56%)
5+
*/
6+
7+
const static auto _ = []() {
8+
cin.tie(nullptr)->sync_with_stdio(false);
9+
return nullptr;
10+
}();
11+
12+
class Solution {
13+
public:
14+
long long findScore(vector<int> &nums) {
15+
int arraySize = nums.size();
16+
vector<pair<int, int>> valueIndexPairs(arraySize);
17+
18+
for (int i = 0; i < arraySize; i++) {
19+
valueIndexPairs[i] = {nums[i], i};
20+
}
21+
22+
sort(valueIndexPairs.begin(), valueIndexPairs.end());
23+
24+
long long totalScore = 0;
25+
26+
for (int i = 0; i < arraySize; i++) {
27+
int currentValue = valueIndexPairs[i].first;
28+
int currentIndex = valueIndexPairs[i].second;
29+
30+
if (nums[currentIndex] != -1) {
31+
totalScore += currentValue;
32+
nums[currentIndex] = -1;
33+
if (currentIndex > 0) {
34+
nums[currentIndex - 1] = -1;
35+
}
36+
if (currentIndex < arraySize - 1) {
37+
nums[currentIndex + 1] = -1;
38+
}
39+
}
40+
}
41+
42+
return totalScore;
43+
}
44+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/
4+
* Runtime: ms (%)
5+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/
4+
* Runtime: ms (%)
5+
"""
6+

0 commit comments

Comments
 (0)