Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# [2593.Find Score of an Array After Marking All Elements][title]

## Description
You are given an array `nums` consisting of positive integers.

Starting with `score = 0`, apply the following algorithm:

- Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
- Add the value of the chosen integer to `score`.
- Mark **the chosen element and its two adjacent elements if they exist**.
- Repeat until all the array elements are marked.

Return the score you get after applying the above algorithm.

**Example 1:**

```
Input: nums = [2,1,3,4,5,2]
Output: 7
Explanation: We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2].
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2].
- 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2].
Our score is 1 + 2 + 4 = 7.
```

**Example 2:**

```
Input: nums = [2,3,5,1,3,2]
Output: 5
Explanation: We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2].
- 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: [2,3,5,1,3,2].
- 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2].
Our score is 1 + 2 + 2 = 5.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
package Solution

func Solution(x bool) bool {
import "container/heap"

type item2593 struct {
v, i int
}

type heap2593 []item2593

func (h *heap2593) Len() int {
return len(*h)
}

func (h *heap2593) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}
func (h *heap2593) Less(i, j int) bool {
a, b := (*h)[i], (*h)[j]
if a.v == b.v {
return a.i < b.i
}
return a.v < b.v
}

func (h *heap2593) Push(x any) {
*h = append(*h, x.(item2593))
}
func (h *heap2593) Pop() any {
old := *h
l := len(old)
x := old[l-1]
*h = old[:l-1]
return x
}

func Solution(nums []int) int64 {
l := len(nums)
used := make([]bool, l)
h := &heap2593{}
for i := range l {
heap.Push(h, item2593{v: nums[i], i: i})
}
ans := int64(0)
for h.Len() > 0 {
top := heap.Pop(h).(item2593)
if used[top.i] {
continue
}
ans += int64(top.v)
if top.i-1 >= 0 {
used[top.i-1] = true
}
if top.i+1 < l {
used[top.i+1] = true
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{2, 1, 3, 4, 5, 2}, 7},
{"TestCase", []int{2, 3, 5, 1, 3, 2}, 5},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading