Skip to content

Commit 57b9177

Browse files
committed
Add solution and test-cases for problem 3066
1 parent d569e0c commit 57b9177

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed

leetcode/3001-3100/3066.Minimum-Operations-to-Exceed-Threshold-Value-II/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [3066.Minimum Operations to Exceed Threshold Value II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** integer array `nums`, and an integer `k`.
5+
6+
In one operation, you will:
7+
8+
- Take the two smallest integers `x` and `y` in nums.
9+
- Remove `x` and `y` from nums.
10+
- Add `min(x, y) * 2 + max(x, y)` anywhere in the array.
11+
12+
**Note** that you can only apply the described operation if `nums` contains at least two elements.
13+
14+
Return the **minimum** number of operations needed so that all elements of the array are greater than or equal to `k`.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: nums = [2,11,10,1,3], k = 10
20+
Output: 2
21+
Explanation: In the first operation, we remove elements 1 and 2, then add 1 * 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
22+
In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
23+
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
24+
It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
1325
```
1426

15-
## 题意
16-
> ...
17-
18-
## 题解
27+
**Example 2:**
1928

20-
### 思路1
21-
> ...
22-
Minimum Operations to Exceed Threshold Value II
23-
```go
2429
```
25-
30+
Input: nums = [1,1,2,4,9], k = 20
31+
Output: 4
32+
Explanation: After one operation, nums becomes equal to [2, 4, 9, 3].
33+
After two operations, nums becomes equal to [7, 4, 9].
34+
After three operations, nums becomes equal to [15, 9].
35+
After four operations, nums becomes equal to [33].
36+
At this stage, all the elements of nums are greater than 20 so we can stop.
37+
It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.
38+
```
2639

2740
## 结语
2841

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
type heap3066 []int
6+
7+
func (h *heap3066) Len() int {
8+
return len(*h)
9+
}
10+
11+
func (h *heap3066) Less(i, j int) bool {
12+
return (*h)[i] < (*h)[j]
13+
}
14+
15+
func (h *heap3066) Swap(i, j int) {
16+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
17+
}
18+
19+
func (h *heap3066) Push(x any) {
20+
*h = append(*h, x.(int))
21+
}
22+
23+
func (h *heap3066) Pop() any {
24+
old := *h
25+
l := len(old)
26+
x := old[l-1]
27+
*h = old[:l-1]
428
return x
529
}
30+
31+
func Solution(nums []int, k int) int {
32+
steps := 0
33+
h := heap3066(nums)
34+
heap.Init(&h)
35+
var a, b int
36+
for h.Len() > 0 {
37+
top := h[0]
38+
if top >= k {
39+
break
40+
}
41+
a = heap.Pop(&h).(int)
42+
b = heap.Pop(&h).(int)
43+
heap.Push(&h, min(a, b)*2+max(a, b))
44+
steps++
45+
}
46+
return steps
47+
}

leetcode/3001-3100/3066.Minimum-Operations-to-Exceed-Threshold-Value-II/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 11, 10, 1, 3}, 10, 2},
18+
{"TestCase2", []int{1, 1, 2, 4, 9}, 20, 4},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)