Skip to content

Commit 88c9e76

Browse files
committed
Add solution and test-cases for problem 2749
1 parent df4288a commit 88c9e76

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [2749.Minimum Operations to Make the Integer Zero][title]
2+
3+
## Description
4+
You are given two integers `num1` and `num2`.
5+
6+
In one operation, you can choose integer `i` in the range `[0, 60]` and subtract `2^i + num2` from `num1`.
7+
8+
Return the integer denoting the **minimum** number of operations needed to make `num1` equal to `0`.
9+
10+
If it is impossible to make `num1` equal to `0`, return `-1`
11+
12+
**Example 1:**
13+
14+
```
15+
Input: num1 = 3, num2 = -2
16+
Output: 3
17+
Explanation: We can make 3 equal to 0 with the following operations:
18+
- We choose i = 2 and subtract 22 + (-2) from 3, 3 - (4 + (-2)) = 1.
19+
- We choose i = 2 and subtract 22 + (-2) from 1, 1 - (4 + (-2)) = -1.
20+
- We choose i = 0 and subtract 20 + (-2) from -1, (-1) - (1 + (-2)) = 0.
21+
It can be proven, that 3 is the minimum number of operations that we need to perform.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: num1 = 5, num2 = 7
28+
Output: -1
29+
Explanation: It can be proven, that it is impossible to make 5 equal to 0 with the given operation.
30+
```
31+
32+
[title]: https://leetcode.com/problems/minimum-operations-to-make-the-integer-zero
33+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math/bits"
5+
)
6+
7+
func canBeSumOfPowersOfTwo(n int64, k int64) bool {
8+
// 1. 如果 n < k,则无法组成。因为每个 2^i 至少是 1 (2^0)。
9+
if n < k {
10+
return false
11+
}
12+
13+
// 2. 计算 n 的二进制表示中 1 的个数,即所需的最小 2^i 数量。
14+
minK := int64(bits.OnesCount64(uint64(n)))
15+
16+
// 3. 检查 k 是否在 [minK, n] 范围内。
17+
// 任何在 minK 和 n 之间的 k 值都是可达的,
18+
// 因为每次拆分一个 2^i 都会让总数增加 1,直到达到 n。
19+
return k >= minK && k <= n
20+
}
21+
22+
func Solution(num1 int, num2 int) int {
23+
// 这个可定是无法完成的
24+
if num2 >= num1 {
25+
return -1
26+
}
27+
an, bn := int64(num1), int64(num2)
28+
i := int64(1)
29+
for ; i < 61; i++ {
30+
an -= bn
31+
if canBeSumOfPowersOfTwo(an, i) {
32+
return int(i)
33+
}
34+
}
35+
return -1
536
}

leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution_test.go

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

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.nums1, c.nums2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.nums1, c.nums2)
2827
}
2928
})
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)