Skip to content

Commit 4c1029a

Browse files
authored
Merge pull request #845 from 0xff-dev/1769
Add solution and test-cases for problem 1769
2 parents 156d315 + 309839f commit 4c1029a

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

leetcode/1701-1800/1769.Minimum-Number-of-Operations-to-Move-All-Balls-to-Each-Box/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [1769.Minimum Number of Operations to Move All Balls to Each Box][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
4+
You have `n` boxes. You are given a binary string `boxes` of length `n`, where `boxes[i]` is '0' if the i<sup>th</sup> box is **empty**, and `'1'` if it contains **one** ball.
5+
6+
In one operation, you can move **one** ball from a box to an adjacent box. Box `i` is adjacent to box `j` if `abs(i - j) == 1`. Note that after doing so, there may be more than one ball in some boxes.
7+
8+
Return an array `answer` of size `n`, where `answer[i]` is the **minimum** number of operations needed to move all the balls to the i<sup>th</sup> box.
9+
10+
Each `answer[i]` is calculated considering the **initial** state of the boxes.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: boxes = "110"
16+
Output: [1,1,3]
17+
Explanation: The answer for each box is as follows:
18+
1) First box: you will have to move one ball from the second box to the first box in one operation.
19+
2) Second box: you will have to move one ball from the first box to the second box in one operation.
20+
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Number of Operations to Move All Balls to Each Box
23-
```go
2425
```
25-
26+
Input: boxes = "001011"
27+
Output: [11,8,5,4,3,4]
28+
```
2629

2730
## 结语
2831

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(boxes string) []int {
4+
ans := make([]int, len(boxes))
5+
pos := make([]int, 0)
6+
for i := 0; i < len(boxes); i++ {
7+
if boxes[i] == '1' {
8+
pos = append(pos, i)
9+
}
10+
}
11+
for i := 0; i < len(boxes); i++ {
12+
for _, p := range pos {
13+
diff := p - i
14+
if diff < 0 {
15+
diff = -diff
16+
}
17+
ans[i] += diff
18+
}
19+
}
20+
return ans
521
}

leetcode/1701-1800/1769.Minimum-Number-of-Operations-to-Move-All-Balls-to-Each-Box/Solution_test.go

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

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)