Skip to content

Commit b676814

Browse files
committed
class UnionFind<T> {
1 parent 1e0ace4 commit b676814

File tree

11 files changed

+127
-60
lines changed

11 files changed

+127
-60
lines changed

.github/workflows/maven.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v3
19-
- name: Set up JDK 19
20-
uses: actions/setup-java@v3
21-
with:
22-
java-version: '19'
23-
distribution: 'temurin'
24-
cache: maven
25-
- name: test with Maven
26-
run: mvn test --file pom.xml
18+
- uses: actions/checkout@v3
19+
- name: Set up JDK 19
20+
uses: actions/setup-java@v3
21+
with:
22+
java-version: '19'
23+
distribution: 'temurin'
24+
cache: maven
25+
- name: test with Maven
26+
run: mvn test --file pom.xml

count-complete-tree-nodes/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/masx200/leetcode-test/count-complete-tree-nodes
22

3-
go 1.19
3+
go 1.19
44

5-
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220906144335-3503ebe76485
5+
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220906144335-3503ebe76485

deepest-leaves-sum/go.mod

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
module github.com/masx200/leetcode-test/deepest-leaves-sum
22

3-
go 1.19
3+
go 1.19
44

5-
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2
5+
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2
66

77

8+
require (
9+
github.com/masx200/leetcode-test/utils v0.0.0-20220825072430-380d3e0e30a2
10+
gotest.tools/v3 v3.3.0
11+
)
812

9-
require (
10-
github.com/masx200/leetcode-test/utils v0.0.0-20220825072430-380d3e0e30a2
11-
gotest.tools/v3 v3.3.0
12-
)
13-
14-
require (
15-
gitee.com/masx200/TreeNode v1.0.4 // indirect
16-
github.com/google/go-cmp v0.5.8 // indirect
17-
)
13+
require (
14+
gitee.com/masx200/TreeNode v1.0.4 // indirect
15+
github.com/google/go-cmp v0.5.8 // indirect
16+
)

go.mod

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
module github.com/masx200/leetcode-test
22

3-
go 1.18
3+
go 1.18
44

5-
require (
6-
github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2
7-
github.com/masx200/leetcode-test/utils v0.0.0-20220825072430-380d3e0e30a2
8-
gotest.tools/v3 v3.3.0
9-
)
5+
require (
6+
github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2
7+
github.com/masx200/leetcode-test/utils v0.0.0-20220825072430-380d3e0e30a2
8+
gotest.tools/v3 v3.3.0
9+
)
1010

11-
require github.com/akrennmair/slice v0.0.0-20220105203817-49445747ab81
11+
require github.com/akrennmair/slice v0.0.0-20220105203817-49445747ab81
1212

13-
require (
14-
gitee.com/masx200/TreeNode v1.0.4 // indirect
15-
github.com/davecgh/go-spew v1.1.1 // indirect
16-
github.com/pmezard/go-difflib v1.0.0 // indirect
17-
gopkg.in/yaml.v3 v3.0.1 // indirect
18-
)
13+
require (
14+
gitee.com/masx200/TreeNode v1.0.4 // indirect
15+
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
gopkg.in/yaml.v3 v3.0.1 // indirect
18+
)
1919

20-
require (
21-
github.com/google/go-cmp v0.5.8 // indirect
22-
github.com/stretchr/testify v1.8.0
23-
)
20+
require (
21+
github.com/google/go-cmp v0.5.8 // indirect
22+
github.com/stretchr/testify v1.8.0
23+
)

maximum-binary-tree-ii/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/masx200/leetcode-test/maximum-binary-tree-ii
22

3-
go 1.19
3+
go 1.19
44

5-
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2
5+
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2

maximum-binary-tree/go.mod

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module github.com/masx200/leetcode-test/maximum-binary-tree
22

3-
go 1.19
3+
go 1.19
44

5-
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2
5+
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2
66

7-
require (
8-
github.com/magiconair/properties v1.8.6
9-
github.com/masx200/leetcode-test/maximum-binary-tree-ii v0.0.0-20221013013845-c54bd13d5663
10-
)
7+
require (
8+
github.com/magiconair/properties v1.8.6
9+
github.com/masx200/leetcode-test/maximum-binary-tree-ii v0.0.0-20221013013845-c54bd13d5663
10+
)
1111

12-
require (
13-
gitee.com/masx200/TreeNode v1.0.4 // indirect
14-
github.com/masx200/leetcode-test/utils v0.0.0-20220825072430-380d3e0e30a2
15-
)
12+
require (
13+
gitee.com/masx200/TreeNode v1.0.4 // indirect
14+
github.com/masx200/leetcode-test/utils v0.0.0-20220825072430-380d3e0e30a2
15+
)

min-cost-to-connect-all-points/index.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package masx200.leetcode_test.min_cost_to_connect_all_points
22

3+
import kotlin.math.abs
4+
import kotlin.math.min
5+
36
class Solution {
47
fun minCostConnectPoints(points: Array<IntArray>): Int {
58
val p0 = points[0]
69
val ds = points.mapIndexed { i, p -> Pair(i, 曼哈顿距离(p, p0)) }.toMap(mutableMapOf())
710
ds.remove(0)
811
var ans = 0
9-
while (ds.size != 0) {
12+
while (ds.isNotEmpty()) {
1013
var mi = 0
1114
var md = Int.MAX_VALUE
1215

@@ -25,7 +28,7 @@ class Solution {
2528
ds.onEach { (i, d) ->
2629

2730
val p = points[i]
28-
ds.set(i, Math.min(d, 曼哈顿距离(p, p1)))
31+
ds.set(i, min(d, 曼哈顿距离(p, p1)))
2932
}
3033
}
3134
return ans
@@ -34,5 +37,5 @@ class Solution {
3437
}
3538

3639
fun 曼哈顿距离(a: IntArray, b: IntArray): Int {
37-
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1])
40+
return abs(a[0] - b[0]) + abs(a[1] - b[1])
3841
}

possible-bipartition/UnionFind.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package masx200.leetcode_test.possible_bipartition
2+
3+
class UnionFind<T> {
4+
fun union(a: T, b: T) {
5+
val fa = this.find(a)
6+
val fb = this.find(b)
7+
if (fa == fb) {
8+
return
9+
}
10+
val sa = this.sizes[fa] ?: 1
11+
val sb = this.sizes[fb] ?: 1
12+
13+
if (sa < sb) {
14+
this.parents[fa] = fb
15+
this.sizes[fb] = sb + sa
16+
} else {
17+
this.parents[fb] = fa
18+
this.sizes[fa] = sb + sa
19+
}
20+
}
21+
22+
private val sizes = mutableMapOf<T, Int>()
23+
private val parents = mutableMapOf<T, T>()
24+
fun connected(p: T, q: T): Boolean {
25+
return this.find(p) == this.find(q)
26+
}
27+
28+
fun find(x: T): T {
29+
if (x != (this.parents[x] ?: x)) {
30+
this.parents[x] = this.find(this.parents[x] ?: x)
31+
32+
}
33+
return this.parents[x] ?: x
34+
}
35+
36+
fun size(x: T): Int {
37+
return this.sizes.getOrDefault(x, 1)
38+
39+
}
40+
}

possible-bipartition/index.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
package masx200.leetcode_test.possible_bipartition
2+
3+
class Solution {
4+
fun possibleBipartition(n: Int, dislikes: Array<IntArray>): Boolean {
5+
val uf = UnionFind<Int>()
6+
7+
val e = Array<MutableList<Int>>(n + 1) { mutableListOf() }
8+
9+
for ((a, b) in dislikes) {
10+
e[a].add(b)
11+
12+
e[b].add(a)
13+
}
14+
15+
e.forEachIndexed { i, v ->
16+
17+
for (d in v) {
18+
uf.union(d, v[0])
19+
if (uf.connected(d, i)) return false
20+
21+
}
22+
}
23+
return true
24+
}
25+
}
26+
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree
22

3-
go 1.18
3+
go 1.18
44

5-
require gotest.tools/v3 v3.3.0
5+
require gotest.tools/v3 v3.3.0
66

7-
require github.com/google/go-cmp v0.5.8 // indirect
7+
require github.com/google/go-cmp v0.5.8 // indirect

0 commit comments

Comments
 (0)