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 2938 #1008

Merged
merged 1 commit into from
Oct 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
40 changes: 27 additions & 13 deletions leetcode/2901-3000/2938.Separate-Black-and-White-Balls/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
# [2938.Separate Black and White 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
There are `n` balls on a table, each ball has a color black or white.

You are given a **0-indexed** binary string `s` of length `n`, where `1` and `0` represent black and white balls, respectively.

In each step, you can choose two adjacent balls and swap them.

Return the **minimum** number of steps to group all the black balls to the right and all the white balls to the left.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "101"
Output: 1
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "011".
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Separate Black and White Balls
```go
```
Input: s = "100"
Output: 2
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "010".
- Swap s[1] and s[2], s = "001".
It can be proven that the minimum number of steps needed is 2.
```

**Example 3:**

```
Input: s = "0111"
Output: 0
Explanation: All the black balls are already grouped to the right.
```

## 结语

Expand Down
12 changes: 10 additions & 2 deletions leetcode/2901-3000/2938.Separate-Black-and-White-Balls/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) int64 {
index := -1
dis := int64(0)
for i := range s {
if s[i] == '0' {
index++
dis += int64(i - index)
}
}
return dis
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "101", 1},
{"TestCase2", "100", 2},
{"TestCase3", "0111", 0},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

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

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