2593. Find Score of an Array After Marking All Elements Medium
1/**
2 * [2593] Find Score of an Array After Marking All Elements
3 *
4 * You are given an array nums consisting of positive integers.
5 * Starting with score = 0, apply the following algorithm:
6 *
7 * Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
8 * Add the value of the chosen integer to score.
9 * Mark the chosen element and its two adjacent elements if they exist.
10 * Repeat until all the array elements are marked.
11 *
12 * Return the score you get after applying the above algorithm.
13 *
14 * <strong class="example">Example 1:
15 *
16 * Input: nums = [2,1,3,4,5,2]
17 * Output: 7
18 * Explanation: We mark the elements as follows:
19 * - 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].
20 * - 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>].
21 * - 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>].
22 * Our score is 1 + 2 + 4 = 7.
23 *
24 * <strong class="example">Example 2:
25 *
26 * Input: nums = [2,3,5,1,3,2]
27 * Output: 5
28 * Explanation: We mark the elements as follows:
29 * - 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].
30 * - 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].
31 * - 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>].
32 * Our score is 1 + 2 + 2 = 5.
33 *
34 *
35 * Constraints:
36 *
37 * 1 <= nums.length <= 10^5
38 * 1 <= nums[i] <= 10^6
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/
44// discuss: https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn find_score(nums: Vec<i32>) -> i64 {
50
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_2593() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.