Skip to content

Commit 02f17df

Browse files
committed
feat: 🎸 leetcode 785
1 parent b756c1b commit 02f17df

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

src/0001-1000/785/isBipartite2_a.ts

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { visitingStatus } from 'constants/vistingStatus'
2+
3+
const { VISITED: WHITE, VISITING: BLACK, INITIAL } = visitingStatus
4+
5+
// <Iteration, BFS, Graph Coloring>
6+
// Time: O(n + m)
7+
// Space: O(n)
8+
// n for the number of nodes
9+
// m for the number of edges
10+
11+
function getOppositeColor(color: number): number {
12+
return color === WHITE ? BLACK : WHITE
13+
}
14+
15+
class MyGraph {
16+
private n: number
17+
private graph: number[][]
18+
private status: number[]
19+
20+
public constructor(graph: number[][]) {
21+
this.n = graph.length
22+
this.graph = graph
23+
this.status = new Array(this.n).fill(INITIAL)
24+
}
25+
26+
// 1.bfs
27+
public bfs(): boolean {
28+
for (let i = 0; i < this.n; ++i) {
29+
if (this.status[i] === INITIAL) {
30+
if (!this.processCurNode(i)) {
31+
return false
32+
}
33+
}
34+
}
35+
36+
return true
37+
}
38+
39+
private processCurNode(i: number): boolean {
40+
const queue: number[] = [i]
41+
42+
this.status[i] = WHITE
43+
44+
while (queue.length) {
45+
const curNode = queue.shift()!
46+
const neighborColor = getOppositeColor(this.status[curNode])
47+
48+
if (!this.processNeighbor(curNode, neighborColor, queue)) {
49+
return false
50+
}
51+
}
52+
53+
return true
54+
}
55+
56+
private processNeighbor(i: number, color: number, queue: number[]): boolean {
57+
for (const neighbor of this.graph[i]) {
58+
if (this.status[neighbor] === INITIAL) {
59+
this.status[neighbor] = color
60+
queue.push(neighbor)
61+
continue
62+
}
63+
64+
if (this.status[neighbor] !== color) {
65+
return false
66+
}
67+
}
68+
69+
return true
70+
}
71+
}
72+
73+
function isBipartite(graph: number[][]): boolean {
74+
// edge cases
75+
if (!graph.length) {
76+
return true
77+
}
78+
79+
const myGraph = new MyGraph(graph)
80+
81+
return myGraph.bfs()
82+
}
83+
84+
export { isBipartite }

src/0001-1000/785/isBipartite_a.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { visitingStatus } from 'constants/vistingStatus'
2+
3+
const { VISITED: WHITE, VISITING: BLACK, INITIAL } = visitingStatus
4+
5+
// <Recursion, DFS, Graph Coloring>
6+
// Time: O(n + m)
7+
// Space: O(n)
8+
// n for the number of nodes
9+
// m for the number of edges
10+
11+
function getOppositeColor(color: number): number {
12+
return color === WHITE ? BLACK : WHITE
13+
}
14+
15+
class MyGraph {
16+
private n: number
17+
private graph: number[][]
18+
private isBipartite: boolean
19+
private status: number[]
20+
21+
public constructor(graph: number[][]) {
22+
this.n = graph.length
23+
this.graph = graph
24+
this.isBipartite = true
25+
this.status = new Array(this.n).fill(INITIAL)
26+
this.initStatus()
27+
}
28+
29+
private initStatus() {
30+
for (let i = 0; i < this.n; ++i) {
31+
if (this.status[i] === INITIAL) {
32+
this.dfs(i, WHITE)
33+
}
34+
}
35+
}
36+
37+
// 1. dfs
38+
private dfs(i: number, status: number) {
39+
this.status[i] = status
40+
41+
for (const neighbor of this.graph[i]) {
42+
if (this.status[neighbor] === INITIAL) {
43+
this.dfs(neighbor, getOppositeColor(status))
44+
}
45+
46+
if (this.status[neighbor] === status) {
47+
this.isBipartite = false
48+
return
49+
}
50+
}
51+
}
52+
53+
public getIsBipartite(): boolean {
54+
return this.isBipartite
55+
}
56+
}
57+
58+
function isBipartite(graph: number[][]): boolean {
59+
// edge cases
60+
if (!graph.length) {
61+
return true
62+
}
63+
64+
const myGraph = new MyGraph(graph)
65+
66+
return myGraph.getIsBipartite()
67+
}
68+
69+
export { isBipartite }

0 commit comments

Comments
 (0)