From 88c9e761cbe3a8242a6c9056e093f8bc4d91c81b Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 5 Sep 2025 09:09:01 +0800 Subject: [PATCH] Add solution and test-cases for problem 2749 --- .../README.md | 33 +++++++++++++++++ .../Solution.go | 35 +++++++++++++++++-- .../Solution_test.go | 21 ++++++----- 3 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/README.md diff --git a/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/README.md b/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/README.md new file mode 100644 index 000000000..991e71e4a --- /dev/null +++ b/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/README.md @@ -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 diff --git a/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution.go b/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution.go index d115ccf5e..6b0d1bb1b 100755 --- a/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution.go +++ b/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution.go @@ -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 } diff --git a/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution_test.go b/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution_test.go index 14ff50eb4..59f649b87 100755 --- a/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution_test.go +++ b/leetcode/2701-2800/2749.Minimum-Operations-to-Make-the-Integer-Zero/Solution_test.go @@ -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() { }