Skip to content

Commit 6bda3ef

Browse files
committed
Add solution and test-cases for problem 2169
1 parent f03226b commit 6bda3ef

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

leetcode/2101-2200/2169.Count-Operations-to-Obtain-Zero/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [2169.Count Operations to Obtain Zero][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
74

5+
You are given two **non-negative** integers `num1` and `num2`.
6+
7+
In one **operation**, if `num1 >= num2`, you must subtract `num2` from `num1`, otherwise subtract `num1` from `num2`.
8+
9+
- For example, if `num1 = 5` and `num2 = 4`, subtract `num2` from `num1`, thus obtaining `num1 = 1` and `num2 = 4`. However, if `num1 = 4` and `num2 = 5`, after one operation, `num1 = 4` and `num2 = 1`.
10+
11+
Return the **number of operations** required to make either `num1 = 0` or `num2 = 0`.
12+
813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: num1 = 2, num2 = 3
17+
Output: 3
18+
Explanation:
19+
- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
20+
- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
21+
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
22+
Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
23+
So the total number of operations required is 3.
1324
```
1425

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

20-
### 思路1
21-
> ...
22-
Count Operations to Obtain Zero
23-
```go
2428
```
25-
29+
Input: num1 = 10, num2 = 10
30+
Output: 1
31+
Explanation:
32+
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
33+
Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
34+
So the total number of operations required is 1.
35+
```
2636

2737
## 结语
2838

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(num1 int, num2 int) int {
4+
var ret, x int
5+
for num1 > 0 && num2 > 0 {
6+
if num1 > num2 {
7+
x = num1 / num2
8+
ret += x
9+
num1 -= num2 * x
10+
continue
11+
}
12+
x = num2 / num1
13+
ret += x
14+
num2 -= num1 * x
15+
}
16+
return ret
517
}

leetcode/2101-2200/2169.Count-Operations-to-Obtain-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+
num1, num2 int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 2, 3, 3},
17+
{"TestCase2", 10, 10, 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.num1, c.num2)
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.num1, c.num2)
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)