Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9217681

Browse files
committed
Merge pull request #382 from 0xff-dev/886
Add solution and test-cases for problem 886
2 parents d52913b + dfd9699 commit 9217681

File tree

3 files changed

+64
-26
lines changed

3 files changed

+64
-26
lines changed

‎leetcode/801-900/0886.Possible-Bipartition/README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [886.Possible Bipartition][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+
We want to split a group of `n` people (labeled from `1` to `n`) into two groups of **any size**. Each person may dislike some other people, and they should not go into the same group.
5+
6+
Given the integer `n` and the array `dislikes` where `dislikes[i] = [ai, bi]` indicates that the person labeled a<sub>i</sub> does not like the person labeled b<sub>i</sub>, return `true` if it is possible to split everyone into two groups in this way.
7+
78

89
**Example 1:**
910

1011
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
12+
Input: n = 4, dislikes = [[1,2],[1,3],[2,4]]
13+
Output: true
14+
Explanation: group1 [1,4] and group2 [2,3].
1315
```
1416

15-
## 题意
16-
> ...
17-
18-
## 题解
17+
**Example 2:**
1918

20-
### 思路1
21-
> ...
22-
Possible Bipartition
23-
```go
2419
```
20+
Input: n = 3, dislikes = [[1,2],[1,3],[2,3]]
21+
Output: false
22+
```
23+
24+
**Example 3:**
2525

26+
```
27+
Input: n = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]]
28+
Output: false
29+
```
2630

2731
## 结语
2832

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+
func Solution(n int, dislikes [][]int) bool {
4+
dislike := make([][]bool, n+1)
5+
for i := 0; i < n+1; i++ {
6+
dislike[i] = make([]bool, n+1)
7+
}
8+
for _, pair := range dislikes {
9+
dislike[pair[0]][pair[1]] = true
10+
dislike[pair[1]][pair[0]] = true
11+
}
12+
13+
var dfs func(int, int) bool
14+
color := make([]int, n+1)
15+
dfs = func(i, nodeColor int) bool {
16+
color[i] = nodeColor
17+
for rel := 1; rel < len(dislike[i]); rel++ {
18+
if dislike[i][rel] && color[rel] == nodeColor {
19+
return false
20+
}
21+
if dislike[i][rel] && color[rel] == 0 {
22+
if !dfs(rel, 3-nodeColor) {
23+
return false
24+
}
25+
}
26+
}
27+
return true
28+
}
29+
30+
for p := 1; p <= n; p++ {
31+
if color[p] == 0 {
32+
if !dfs(p, 1) {
33+
return false
34+
}
35+
}
36+
}
37+
return true
538
}

‎leetcode/801-900/0886.Possible-Bipartition/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n int
14+
dislikes [][]int
15+
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 4, [][]int{{1, 2}, {1, 3}, {2, 4}}, true},
18+
{"TestCase2", 3, [][]int{{1, 2}, {1, 3}, {2, 3}}, false},
19+
{"TestCase5", 5, [][]int{{1, 2}, {2, 3}, {3, 4}, {4, 5}, {1, 5}}, false},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.dislikes)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.n, c.dislikes)
2829
}
2930
})
3031
}
3132
}
3233

33-
//压力测试
34+
//压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
//使用案列
38+
//使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /