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 2134 #947

Merged
merged 1 commit into from
Sep 9, 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
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
# [2134.Minimum Swaps to Group All 1's Together II][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
A **swap** is defined as taking two **distinct** positions in an array and swapping the values in them.

A **circular** array is defined as an array where we consider the **first** element and the **last** element to be **adjacent**.

Given a **binary circular** array `nums`, return the minimum number of swaps required to group all `1`'s present in the array together at **any location**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [0,1,0,1,1,0,0]
Output: 1
Explanation: Here are a few of the ways to group all the 1's together:
[0,0,1,1,1,0,0] using 1 swap.
[0,1,1,1,0,0,0] using 1 swap.
[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
There is no way to group all 1's together with 0 swaps.
Thus, the minimum number of swaps required is 1.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Minimum Swaps to Group All 1's Together II
```go
```
Input: nums = [0,1,1,1,0,0,1,1,0]
Output: 2
Explanation: Here are a few of the ways to group all the 1's together:
[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
[1,1,1,1,1,0,0,0,0] using 2 swaps.
There is no way to group all 1's together with 0 or 1 swaps.
Thus, the minimum number of swaps required is 2.
```

**Example 3:**

```
Input: nums = [1,1,0,0,1]
Output: 0
Explanation: All the 1's are already grouped together due to the circular property of the array.
Thus, the minimum number of swaps required is 0.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
one := 0
for _, n := range nums {
if n == 1 {
one++
}
}
l := len(nums)
// 全是1, 0
if one == l || one == 0 {
return 0
}
// 0, 0, 1, 1, 0, 1
// (0, 2), (1,3),(2,4),(3,5),(4,0),(5,1)
// 0, 1, 2, 3, 4, 5
start, end := l-one+1, 0
ones := 0

for i := start; i != end+1; i = (i + 1) % l {
if nums[i] == 1 {
ones++
}
}
cur := ones
if nums[start] == 1 {
ones--
}
start, end = (start+1)%l, end+1
for end < l {
if nums[end] == 1 {
ones++
}
cur = max(cur, ones)
if nums[start] == 1 {
ones--
}
start, end = (start+1)%l, end+1
}

return one - cur
}
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 []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{0, 1, 0, 1, 1, 0, 0}, 1},
{"TestCase2", []int{0, 1, 1, 1, 0, 0, 1, 1, 0}, 2},
{"TestCase3", []int{1, 1, 0, 0, 1}, 0},
}

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

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

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