Skip to content

Commit 8ac25eb

Browse files
authored
Merge pull request #1062 from 0xff-dev/2593
Add solution and test-cases for problem 2593
2 parents 9eb928b + 81ec328 commit 8ac25eb

File tree

3 files changed

+105
-8
lines changed

3 files changed

+105
-8
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [2593.Find Score of an Array After Marking All Elements][title]
2+
3+
## Description
4+
You are given an array `nums` consisting of positive integers.
5+
6+
Starting with `score = 0`, apply the following algorithm:
7+
8+
- Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
9+
- Add the value of the chosen integer to `score`.
10+
- Mark **the chosen element and its two adjacent elements if they exist**.
11+
- Repeat until all the array elements are marked.
12+
13+
Return the score you get after applying the above algorithm.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: nums = [2,1,3,4,5,2]
19+
Output: 7
20+
Explanation: We mark the elements as follows:
21+
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2].
22+
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2].
23+
- 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2].
24+
Our score is 1 + 2 + 4 = 7.
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: nums = [2,3,5,1,3,2]
31+
Output: 5
32+
Explanation: We mark the elements as follows:
33+
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2].
34+
- 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].
35+
- 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2].
36+
Our score is 1 + 2 + 2 = 5.
37+
```
38+
39+
## 结语
40+
41+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
42+
43+
[title]: https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements
44+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
type item2593 struct {
6+
v, i int
7+
}
8+
9+
type heap2593 []item2593
10+
11+
func (h *heap2593) Len() int {
12+
return len(*h)
13+
}
14+
15+
func (h *heap2593) Swap(i, j int) {
16+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
17+
}
18+
func (h *heap2593) Less(i, j int) bool {
19+
a, b := (*h)[i], (*h)[j]
20+
if a.v == b.v {
21+
return a.i < b.i
22+
}
23+
return a.v < b.v
24+
}
25+
26+
func (h *heap2593) Push(x any) {
27+
*h = append(*h, x.(item2593))
28+
}
29+
func (h *heap2593) Pop() any {
30+
old := *h
31+
l := len(old)
32+
x := old[l-1]
33+
*h = old[:l-1]
434
return x
535
}
36+
37+
func Solution(nums []int) int64 {
38+
l := len(nums)
39+
used := make([]bool, l)
40+
h := &heap2593{}
41+
for i := range l {
42+
heap.Push(h, item2593{v: nums[i], i: i})
43+
}
44+
ans := int64(0)
45+
for h.Len() > 0 {
46+
top := heap.Pop(h).(item2593)
47+
if used[top.i] {
48+
continue
49+
}
50+
ans += int64(top.v)
51+
if top.i-1 >= 0 {
52+
used[top.i-1] = true
53+
}
54+
if top.i+1 < l {
55+
used[top.i+1] = true
56+
}
57+
}
58+
return ans
59+
}

leetcode/2501-2600/2593.Find-Score-of-an-Array-After-Marking-All-Elements/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 1, 3, 4, 5, 2}, 7},
17+
{"TestCase", []int{2, 3, 5, 1, 3, 2}, 5},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)