Skip to content

Add solution and test-cases for problem 3066 #1121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
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
@@ -1,28 +1,41 @@
# [3066.Minimum Operations to Exceed Threshold Value II][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a **0-indexed** integer array `nums`, and an integer `k`.

In one operation, you will:

- Take the two smallest integers `x` and `y` in nums.
- Remove `x` and `y` from nums.
- Add `min(x, y) * 2 + max(x, y)` anywhere in the array.

**Note** that you can only apply the described operation if `nums` contains at least two elements.

Return the **minimum** number of operations needed so that all elements of the array are greater than or equal to `k`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [2,11,10,1,3], k = 10
Output: 2
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].
In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
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.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Minimum Operations to Exceed Threshold Value II
```go
```

Input: nums = [1,1,2,4,9], k = 20
Output: 4
Explanation: After one operation, nums becomes equal to [2, 4, 9, 3].
After two operations, nums becomes equal to [7, 4, 9].
After three operations, nums becomes equal to [15, 9].
After four operations, nums becomes equal to [33].
At this stage, all the elements of nums are greater than 20 so we can stop.
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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
package Solution

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

type heap3066 []int

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

func (h *heap3066) Less(i, j int) bool {
return (*h)[i] < (*h)[j]
}

func (h *heap3066) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}

func (h *heap3066) Push(x any) {
*h = append(*h, x.(int))
}

func (h *heap3066) Pop() any {
old := *h
l := len(old)
x := old[l-1]
*h = old[:l-1]
return x
}

func Solution(nums []int, k int) int {
steps := 0
h := heap3066(nums)
heap.Init(&h)
var a, b int
for h.Len() > 0 {
top := h[0]
if top >= k {
break
}
a = heap.Pop(&h).(int)
b = heap.Pop(&h).(int)
heap.Push(&h, min(a, b)*2+max(a, b))
steps++
}
return steps
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{2, 11, 10, 1, 3}, 10, 2},
{"TestCase2", []int{1, 1, 2, 4, 9}, 20, 4},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.k)
}
})
}
}

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

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