Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1552 #912

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions leetcode/1501-1600/1552.Magnetic-Force-Between-Two-Balls/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# [1552.Magnetic Force Between Two Balls][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
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**.

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

```
Input: a = "11", b = "1"
Output: "100"
```
Given the integer array `position` and the integer `m`. Return the required force.

## 题意
> ...
**Example 1:**

## 题解
![1](./q3v1.jpeg)

### 思路1
> ...
Magnetic Force Between Two Balls
```go
```
Input: position = [1,2,3,4,7], m = 3
Output: 3
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.
```

**Example 2:**

```
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func canPlaceBalls(x int, position []int, m int) bool {
prevBallPos := position[0]
ballsPlaced := 1

for i := 1; i < len(position) && ballsPlaced < m; i++ {
currPos := position[i]
if currPos-prevBallPos >= x {
ballsPlaced++
prevBallPos = currPos
}
}

return ballsPlaced == m
}

func Solution(position []int, m int) int {
answer := 0
n := len(position)
sort.Ints(position)

low := 1
high := int(position[n-1]) / (m - 1)
for low <= high {
mid := low + (high-low)/2
if canPlaceBalls(mid, position, m) {
answer = mid
low = mid + 1
} else {
high = mid - 1
}
}

return answer
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
p []int
m int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3, 4, 7}, 3, 3},
{"TestCase2", []int{5, 4, 3, 2, 1, 1000000000}, 2, 999999999},
}

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

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading