java.lang.Object
g2501_2600.s2593_find_score_of_an_array_after_marking_all_elements.Solution

public class Solution extends Object
2593 - Find Score of an Array After Marking All Elements.<p>Medium</p> <p>You are given an array <code>nums</code> consisting of positive integers.</p> <p>Starting with <code>score = 0</code>, apply the following algorithm:</p> <ul> <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> <li>Add the value of the chosen integer to <code>score</code>.</li> <li>Mark <strong>the chosen element and its two adjacent elements if they exist</strong>.</li> <li>Repeat until all the array elements are marked.</li> </ul> <p>Return <em>the score you get after applying the above algorithm</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [2,1,3,4,5,2]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> We mark the elements as follows:</p> <ul> <li>1 is the smallest unmarked element, so we mark it and its two adjacent elements: [<ins>2</ins>,<ins>1</ins>,<ins>3</ins>,4,5,2].</li> <li>2 is the smallest unmarked element, so we mark it and its left adjacent element: [<ins>2</ins>,<ins>1</ins>,<ins>3</ins>,4,<ins>5</ins>,<ins>2</ins>].</li> <li>4 is the only remaining unmarked element, so we mark it: [<ins>2</ins>,<ins>1</ins>,<ins>3</ins>,<ins>4</ins>,<ins>5</ins>,<ins>2</ins>]. Our score is 1 + 2 + 4 = 7.</li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,3,5,1,3,2]</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> We mark the elements as follows:</p> <ul> <li>1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,<ins>5</ins>,<ins>1</ins>,<ins>3</ins>,2].</li> <li>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: [<ins>2</ins>,<ins>3</ins>,<ins>5</ins>,<ins>1</ins>,<ins>3</ins>,2].</li> <li>2 is the only remaining unmarked element, so we mark it: [<ins>2</ins>,<ins>3</ins>,<ins>5</ins>,<ins>1</ins>,<ins>3</ins>,<ins>2</ins>]. Our score is 1 + 2 + 2 = 5.</li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findScore

      public long findScore(int[] arr)