|
| 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> </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> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 45 | + <li><code>1 <= nums[i] <= 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. |
0 commit comments