Skip to content

Commit

Permalink
Merge pull request #644 from 0xff-dev/991
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 991
  • Loading branch information
6boris authored Oct 28, 2023
2 parents c790a4a + 839bd21 commit 253494c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
34 changes: 21 additions & 13 deletions leetcode/901-1000/0991.Broken-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [991.Broken Calculator][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
There is a broken calculator that has the integer `startValue` on its display initially. In one operation, you can:

- multiply the number on display by `2`, or
- subtract `1` from the number on display.

Given two integers `startValue` and `target`, return the minimum number of operations needed to display `target` on the calculator.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: startValue = 2, target = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Broken Calculator
```go
```
Input: startValue = 5, target = 8
Output: 2
Explanation: Use decrement and then double {5 -> 4 -> 8}.
```

**Example 3:**

```
Input: startValue = 3, target = 10
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
```

## 结语

Expand Down
13 changes: 11 additions & 2 deletions leetcode/901-1000/0991.Broken-Calculator/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(startValue, target int) int {
ans := 0
for target > startValue {
ans++
if target&1 == 1 {
target++
continue
}
target /= 2
}
return ans + startValue - target
}
22 changes: 11 additions & 11 deletions leetcode/901-1000/0991.Broken-Calculator/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
start, target int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2, 3, 2},
{"TestCase2", 5, 8, 2},
{"TestCase3", 3, 10, 3},
}

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

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

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

0 comments on commit 253494c

Please sign in to comment.