Skip to content

Commit e41af32

Browse files
committed
feat: add solutions to lc problem: No.0886
No.0886.Possible Bipartition
1 parent 75e27a2 commit e41af32

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

solution/0800-0899/0886.Possible Bipartition/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,67 @@ func possibleBipartition(n int, dislikes [][]int) bool {
251251
}
252252
```
253253

254+
### **TypeScript**
255+
256+
```ts
257+
function possibleBipartition(n: number, dislikes: number[][]): boolean {
258+
const color = new Array(n + 1).fill(0);
259+
const g = Array.from({ length: n + 1 }, () => []);
260+
const dfs = (i: number, v: number) => {
261+
color[i] = v;
262+
for (const j of g[i]) {
263+
if (color[j] === color[i] || (color[j] === 0 && dfs(j, 3 ^ v))) {
264+
return true;
265+
}
266+
}
267+
return false;
268+
};
269+
for (const [a, b] of dislikes) {
270+
g[a].push(b);
271+
g[b].push(a);
272+
}
273+
for (let i = 1; i <= n; i++) {
274+
if (color[i] === 0 && dfs(i, 1)) {
275+
return false;
276+
}
277+
}
278+
return true;
279+
}
280+
```
281+
282+
### **Rust**
283+
284+
```rust
285+
impl Solution {
286+
fn dfs(i: usize, v: usize, color: &mut Vec<usize>, g: &Vec<Vec<usize>>) -> bool {
287+
color[i] = v;
288+
for &j in (*g[i]).iter() {
289+
if color[j] == color[i] || color[j] == 0 && Self::dfs(j, v ^ 3, color, g) {
290+
return true;
291+
}
292+
}
293+
false
294+
}
295+
296+
pub fn possible_bipartition(n: i32, dislikes: Vec<Vec<i32>>) -> bool {
297+
let n = n as usize;
298+
let mut color = vec![0; n + 1];
299+
let mut g = vec![Vec::new(); n + 1];
300+
for d in dislikes.iter() {
301+
let (i, j) = (d[0] as usize, d[1] as usize);
302+
g[i].push(j);
303+
g[j].push(i);
304+
}
305+
for i in 1..=n {
306+
if color[i] == 0 && Self::dfs(i, 1, &mut color, &g) {
307+
return false;
308+
}
309+
}
310+
true
311+
}
312+
}
313+
```
314+
254315
### **...**
255316

256317
```

solution/0800-0899/0886.Possible Bipartition/README_EN.md

+61
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,67 @@ func possibleBipartition(n int, dislikes [][]int) bool {
178178
}
179179
```
180180

181+
### **TypeScript**
182+
183+
```ts
184+
function possibleBipartition(n: number, dislikes: number[][]): boolean {
185+
const color = new Array(n + 1).fill(0);
186+
const g = Array.from({ length: n + 1 }, () => []);
187+
const dfs = (i: number, v: number) => {
188+
color[i] = v;
189+
for (const j of g[i]) {
190+
if (color[j] === color[i] || (color[j] === 0 && dfs(j, 3 ^ v))) {
191+
return true;
192+
}
193+
}
194+
return false;
195+
};
196+
for (const [a, b] of dislikes) {
197+
g[a].push(b);
198+
g[b].push(a);
199+
}
200+
for (let i = 1; i <= n; i++) {
201+
if (color[i] === 0 && dfs(i, 1)) {
202+
return false;
203+
}
204+
}
205+
return true;
206+
}
207+
```
208+
209+
### **Rust**
210+
211+
```rust
212+
impl Solution {
213+
fn dfs(i: usize, v: usize, color: &mut Vec<usize>, g: &Vec<Vec<usize>>) -> bool {
214+
color[i] = v;
215+
for &j in (*g[i]).iter() {
216+
if color[j] == color[i] || color[j] == 0 && Self::dfs(j, v ^ 3, color, g) {
217+
return true;
218+
}
219+
}
220+
false
221+
}
222+
223+
pub fn possible_bipartition(n: i32, dislikes: Vec<Vec<i32>>) -> bool {
224+
let n = n as usize;
225+
let mut color = vec![0; n + 1];
226+
let mut g = vec![Vec::new(); n + 1];
227+
for d in dislikes.iter() {
228+
let (i, j) = (d[0] as usize, d[1] as usize);
229+
g[i].push(j);
230+
g[j].push(i);
231+
}
232+
for i in 1..=n {
233+
if color[i] == 0 && Self::dfs(i, 1, &mut color, &g) {
234+
return false;
235+
}
236+
}
237+
true
238+
}
239+
}
240+
```
241+
181242
### **...**
182243

183244
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
fn dfs(i: usize, v: usize, color: &mut Vec<usize>, g: &Vec<Vec<usize>>) -> bool {
3+
color[i] = v;
4+
for &j in (*g[i]).iter() {
5+
if color[j] == color[i] || color[j] == 0 && Self::dfs(j, v ^ 3, color, g) {
6+
return true;
7+
}
8+
}
9+
false
10+
}
11+
12+
pub fn possible_bipartition(n: i32, dislikes: Vec<Vec<i32>>) -> bool {
13+
let n = n as usize;
14+
let mut color = vec![0; n + 1];
15+
let mut g = vec![Vec::new(); n + 1];
16+
for d in dislikes.iter() {
17+
let (i, j) = (d[0] as usize, d[1] as usize);
18+
g[i].push(j);
19+
g[j].push(i);
20+
}
21+
for i in 1..=n {
22+
if color[i] == 0 && Self::dfs(i, 1, &mut color, &g) {
23+
return false;
24+
}
25+
}
26+
true
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function possibleBipartition(n: number, dislikes: number[][]): boolean {
2+
const color = new Array(n + 1).fill(0);
3+
const g = Array.from({ length: n + 1 }, () => []);
4+
const dfs = (i: number, v: number) => {
5+
color[i] = v;
6+
for (const j of g[i]) {
7+
if (color[j] === color[i] || (color[j] === 0 && dfs(j, 3 ^ v))) {
8+
return true;
9+
}
10+
}
11+
return false;
12+
};
13+
for (const [a, b] of dislikes) {
14+
g[a].push(b);
15+
g[b].push(a);
16+
}
17+
for (let i = 1; i <= n; i++) {
18+
if (color[i] === 0 && dfs(i, 1)) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}

0 commit comments

Comments
 (0)