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,33 @@
# [2749.Minimum Operations to Make the Integer Zero][title]

## Description
You are given two integers `num1` and `num2`.

In one operation, you can choose integer `i` in the range `[0, 60]` and subtract `2^i + num2` from `num1`.

Return the integer denoting the **minimum** number of operations needed to make `num1` equal to `0`.

If it is impossible to make `num1` equal to `0`, return `-1`

**Example 1:**

```
Input: num1 = 3, num2 = -2
Output: 3
Explanation: We can make 3 equal to 0 with the following operations:
- We choose i = 2 and subtract 22 + (-2) from 3, 3 - (4 + (-2)) = 1.
- We choose i = 2 and subtract 22 + (-2) from 1, 1 - (4 + (-2)) = -1.
- We choose i = 0 and subtract 20 + (-2) from -1, (-1) - (1 + (-2)) = 0.
It can be proven, that 3 is the minimum number of operations that we need to perform.
```

**Example 2:**

```
Input: num1 = 5, num2 = 7
Output: -1
Explanation: It can be proven, that it is impossible to make 5 equal to 0 with the given operation.
```

[title]: https://leetcode.com/problems/minimum-operations-to-make-the-integer-zero
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
package Solution

func Solution(x bool) bool {
return x
import (
"math/bits"
)

func canBeSumOfPowersOfTwo(n int64, k int64) bool {
// 1. 如果 n < k,则无法组成。因为每个 2^i 至少是 1 (2^0)。
if n < k {
return false
}

// 2. 计算 n 的二进制表示中 1 的个数,即所需的最小 2^i 数量。
minK := int64(bits.OnesCount64(uint64(n)))

// 3. 检查 k 是否在 [minK, n] 范围内。
// 任何在 minK 和 n 之间的 k 值都是可达的,
// 因为每次拆分一个 2^i 都会让总数增加 1,直到达到 n。
return k >= minK && k <= n
}

func Solution(num1 int, num2 int) int {
// 这个可定是无法完成的
if num2 >= num1 {
return -1
}
an, bn := int64(num1), int64(num2)
i := int64(1)
for ; i < 61; i++ {
an -= bn
if canBeSumOfPowersOfTwo(an, i) {
return int(i)
}
}
return -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
nums1, nums2 int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 3, -2, 3},
{"TestCase2", 5, 7, -1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums1, c.nums2)
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.nums1, c.nums2)
}
})
}
}

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

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