Skip to content

Commit e310a6c

Browse files
authored
Merge pull request #912 from 0xff-dev/1552
Add solution and test-cases for problem 1552
2 parents 56cfbe8 + 894ed70 commit e310a6c

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

leetcode/1501-1600/1552.Magnetic-Force-Between-Two-Balls/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [1552.Magnetic Force Between Two Balls][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+
In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has `n` empty baskets, the i<sup>th</sup> basket is at `position[i]`, Morty has `m` balls and needs to distribute the balls into the baskets such that the **minimum magnetic force** between any two balls is **maximum**.
75

8-
**Example 1:**
6+
Rick stated that magnetic force between two different balls at positions `x` and `y` is `|x - y|`.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Given the integer array `position` and the integer `m`. Return the required force.
149

15-
## 题意
16-
> ...
10+
**Example 1:**
1711

18-
## 题解
12+
![1](./q3v1.jpeg)
1913

20-
### 思路1
21-
> ...
22-
Magnetic Force Between Two Balls
23-
```go
14+
```
15+
Input: position = [1,2,3,4,7], m = 3
16+
Output: 3
17+
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
2418
```
2519

20+
**Example 2:**
21+
22+
```
23+
Input: position = [5,4,3,2,1,1000000000], m = 2
24+
Output: 999999999
25+
Explanation: We can use baskets 1 and 1000000000.
26+
```
2627

2728
## 结语
2829

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func canPlaceBalls(x int, position []int, m int) bool {
6+
prevBallPos := position[0]
7+
ballsPlaced := 1
8+
9+
for i := 1; i < len(position) && ballsPlaced < m; i++ {
10+
currPos := position[i]
11+
if currPos-prevBallPos >= x {
12+
ballsPlaced++
13+
prevBallPos = currPos
14+
}
15+
}
16+
17+
return ballsPlaced == m
18+
}
19+
20+
func Solution(position []int, m int) int {
21+
answer := 0
22+
n := len(position)
23+
sort.Ints(position)
24+
25+
low := 1
26+
high := int(position[n-1]) / (m - 1)
27+
for low <= high {
28+
mid := low + (high-low)/2
29+
if canPlaceBalls(mid, position, m) {
30+
answer = mid
31+
low = mid + 1
32+
} else {
33+
high = mid - 1
34+
}
35+
}
36+
37+
return answer
538
}

leetcode/1501-1600/1552.Magnetic-Force-Between-Two-Balls/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
p []int
14+
m int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 3, 4, 7}, 3, 3},
18+
{"TestCase2", []int{5, 4, 3, 2, 1, 1000000000}, 2, 999999999},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.p, c.m)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.p, c.m)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}
Loading

0 commit comments

Comments
 (0)