Skip to content

Commit 312b43c

Browse files
committed
leetcode
1 parent 0d4ffea commit 312b43c

File tree

4 files changed

+295
-0
lines changed

4 files changed

+295
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
3+
-* 886. Possible Bipartition *-
4+
5+
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.
6+
7+
Given the integer n and the array dislikes where dislikes[i] = [ai, bi] indicates that the person labeled ai does not like the person labeled bi, return true if it is possible to split everyone into two groups in this way.
8+
9+
10+
11+
Example 1:
12+
13+
Input: n = 4, dislikes = [[1,2],[1,3],[2,4]]
14+
Output: true
15+
Explanation: group1 [1,4] and group2 [2,3].
16+
Example 2:
17+
18+
Input: n = 3, dislikes = [[1,2],[1,3],[2,3]]
19+
Output: false
20+
Example 3:
21+
22+
Input: n = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]]
23+
Output: false
24+
25+
26+
Constraints:
27+
28+
1 <= n <= 2000
29+
0 <= dislikes.length <= 104
30+
dislikes[i].length == 2
31+
1 <= dislikes[i][j] <= n
32+
ai < bi
33+
All the pairs of dislikes are unique.
34+
35+
36+
*/
37+
38+
import 'dart:collection';
39+
40+
class A {
41+
bool possibleBipartition(int n, List<List<int>> dislikes) {
42+
List<List<int>> graph =
43+
List.filled(n, 0).map((e) => List.filled(n, 0)).toList();
44+
for (List<int> d in dislikes) {
45+
graph[d[0] - 1][d[1] - 1] = 1;
46+
graph[d[1] - 1][d[0] - 1] = 1;
47+
}
48+
List<int> group = List.filled(n, 0);
49+
for (int i = 0; i < n; i++) {
50+
if (group[i] == 0 && !depthFirstSearch(graph, group, i, 1)) {
51+
return false;
52+
}
53+
}
54+
return true;
55+
}
56+
57+
bool depthFirstSearch(
58+
List<List<int>> graph, List<int> group, int index, int g) {
59+
group[index] = g;
60+
for (int i = 0; i < graph.length; i++) {
61+
if (graph[index][i] == 1) {
62+
if (group[i] == g) {
63+
return false;
64+
}
65+
if (group[i] == 0 && !depthFirstSearch(graph, group, i, -g)) {
66+
return false;
67+
}
68+
}
69+
}
70+
return true;
71+
}
72+
}
73+
74+
// breadth first Search
75+
class B {
76+
bool possibleBipartition(int n, List<List<int>> dislikes) {
77+
int len = dislikes.length;
78+
if (len < 2) return true;
79+
List<int> color = List.filled(n + 1, 0);
80+
List<List<int>> graph = [];
81+
for (int i = 0; i <= n; i++) {
82+
List<int> tmp = [];
83+
graph.add(tmp);
84+
}
85+
for (int i = 0; i < len; i++) {
86+
int m = dislikes[i][0];
87+
int n = dislikes[i][1];
88+
graph[m].add(n);
89+
graph[n].add(m);
90+
}
91+
for (int i = 1; i <= n; i++) {
92+
if (color[i] == 0) {
93+
color[i] = 1;
94+
Queue<int> q = Queue();
95+
q.add(i);
96+
while (q.isNotEmpty) {
97+
int cur = q.removeFirst();
98+
for (int j in graph[cur]) {
99+
if (color[j] == 0) {
100+
color[j] = color[cur] == 1 ? 2 : 1;
101+
q.add(j);
102+
} else {
103+
if (color[j] == color[cur]) return false;
104+
}
105+
}
106+
}
107+
}
108+
}
109+
return true;
110+
}
111+
}
112+
113+
class C {
114+
HashMap<int, List<int>> map = HashMap();
115+
bool possibleBipartition(int n, List<List<int>> dislikes) {
116+
for (List<int> dis in dislikes) {
117+
if (map[dis[0]] == null) map[dis[0]] = [];
118+
map[dis[0]]?.add(dis[1]);
119+
if (map[dis[1]] == null) map[dis[1]] = [];
120+
map[dis[1]]?.add(dis[0]);
121+
}
122+
List<int> colors = List.filled(n + 1, 0);
123+
for (int i = 1; i <= n; i++) {
124+
if (map[i] == null) continue;
125+
if (colors[i] == 0 && !helper(i, colors, 1)) return false;
126+
}
127+
return true;
128+
}
129+
130+
bool helper(int index, List<int> colors, int color) {
131+
if (colors[index] != 0) return color == colors[index];
132+
int len = map[index]!.length;
133+
colors[index] = color;
134+
for (int i = 0; i < len; i++) {
135+
if (!helper(map[index]![i], colors, -1 * color)) return false;
136+
}
137+
return true;
138+
}
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
func possibleBipartition(n int, dislikes [][]int) bool {
4+
var graph = make([][]int, n)
5+
6+
for _, pair := range dislikes {
7+
i := pair[0] - 1
8+
j := pair[1] - 1
9+
graph[i] = append(graph[i], j)
10+
graph[j] = append(graph[j], i)
11+
}
12+
13+
var colors = make([]int, n)
14+
15+
for i := 0; i < n; i++ {
16+
if colors[i] == 0 && !isValid(&graph, &colors, i, 1) {
17+
return false
18+
}
19+
}
20+
21+
return true
22+
}
23+
24+
func isValid(graph *[][]int, colors *[]int, i int, color int) bool {
25+
if (*colors)[i] != 0 {
26+
return (*colors)[i] == color
27+
}
28+
29+
(*colors)[i] = color
30+
31+
for _, j := range (*graph)[i] {
32+
if (*colors)[i] == (*colors)[j] || !isValid(graph, colors, j, -color) {
33+
return false
34+
}
35+
}
36+
37+
return true
38+
}
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# 🔥 Possible Bipartition 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 Depth First Search
4+
5+
```dart
6+
class Solution {
7+
bool possibleBipartition(int n, List<List<int>> dislikes) {
8+
List<List<int>> graph =
9+
List.filled(n, 0).map((e) => List.filled(n, 0)).toList();
10+
for (List<int> d in dislikes) {
11+
graph[d[0] - 1][d[1] - 1] = 1;
12+
graph[d[1] - 1][d[0] - 1] = 1;
13+
}
14+
List<int> group = List.filled(n, 0);
15+
for (int i = 0; i < n; i++) {
16+
if (group[i] == 0 && !depthFirstSearch(graph, group, i, 1)) {
17+
return false;
18+
}
19+
}
20+
return true;
21+
}
22+
23+
bool depthFirstSearch(
24+
List<List<int>> graph, List<int> group, int index, int g) {
25+
group[index] = g;
26+
for (int i = 0; i < graph.length; i++) {
27+
if (graph[index][i] == 1) {
28+
if (group[i] == g) {
29+
return false;
30+
}
31+
if (group[i] == 0 && !depthFirstSearch(graph, group, i, -g)) {
32+
return false;
33+
}
34+
}
35+
}
36+
return true;
37+
}
38+
}
39+
```
40+
41+
## Solution - 2 Breadth First Search
42+
43+
```dart
44+
import 'dart:collection';
45+
46+
class Solution {
47+
bool possibleBipartition(int n, List<List<int>> dislikes) {
48+
int len = dislikes.length;
49+
if (len < 2) return true;
50+
List<int> color = List.filled(n + 1, 0);
51+
List<List<int>> graph = [];
52+
for (int i = 0; i <= n; i++) {
53+
List<int> tmp = [];
54+
graph.add(tmp);
55+
}
56+
for (int i = 0; i < len; i++) {
57+
int m = dislikes[i][0];
58+
int n = dislikes[i][1];
59+
graph[m].add(n);
60+
graph[n].add(m);
61+
}
62+
for (int i = 1; i <= n; i++) {
63+
if (color[i] == 0) {
64+
color[i] = 1;
65+
Queue<int> q = Queue();
66+
q.add(i);
67+
while (q.isNotEmpty) {
68+
int cur = q.removeFirst();
69+
for (int j in graph[cur]) {
70+
if (color[j] == 0) {
71+
color[j] = color[cur] == 1 ? 2 : 1;
72+
q.add(j);
73+
} else {
74+
if (color[j] == color[cur]) return false;
75+
}
76+
}
77+
}
78+
}
79+
}
80+
return true;
81+
}
82+
}
83+
```
84+
85+
## Solution - 3 Memoization
86+
87+
```dart
88+
import 'dart:collection';
89+
90+
class Solution {
91+
HashMap<int, List<int>> map = HashMap();
92+
bool possibleBipartition(int n, List<List<int>> dislikes) {
93+
for (List<int> dis in dislikes) {
94+
if (map[dis[0]] == null) map[dis[0]] = [];
95+
map[dis[0]]?.add(dis[1]);
96+
if (map[dis[1]] == null) map[dis[1]] = [];
97+
map[dis[1]]?.add(dis[0]);
98+
}
99+
List<int> colors = List.filled(n + 1, 0);
100+
for (int i = 1; i <= n; i++) {
101+
if (map[i] == null) continue;
102+
if (colors[i] == 0 && !helper(i, colors, 1)) return false;
103+
}
104+
return true;
105+
}
106+
107+
bool helper(int index, List<int> colors, int color) {
108+
if (colors[index] != 0) return color == colors[index];
109+
int len = map[index]!.length;
110+
colors[index] = color;
111+
for (int i = 0; i < len; i++) {
112+
if (!helper(map[index]![i], colors, -1 * color)) return false;
113+
}
114+
return true;
115+
}
116+
}
117+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
172172
- [**739.** Daily Temperatures](DailyTemperatures/daily_temperatures.dart)
173173
- [**1971.* Find if Path Exists in Graph](FindIfPathExistsInGraph/find_if_path_exists_in_graph.dart)
174174
- [**841.** Keys and Rooms](KeysAndRooms/keys_and_rooms.dart)
175+
- [**886.** Possible Bipartition](PossibleBipartition/possible_bipartition.dart)
175176

176177
## Reach me via
177178

0 commit comments

Comments
 (0)