-
Notifications
You must be signed in to change notification settings - Fork 0
/
_20220804.kt
195 lines (163 loc) · 5.47 KB
/
_20220804.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@file:Suppress("DuplicatedCode", "ClassName", "ObjectPropertyName")
package _2022._08
import Testable
import utils.UnionFind
import utils.assertEqualTo
import utils.runTimedTests
import java.util.LinkedList
// 40. Combination Sum II
// https://leetcode.com/problems/combination-sum-ii/
private interface Leetcode_40 {
fun combinationSum2(candidates: IntArray, target: Int): List<List<Int>>
companion object : Testable {
override fun test() {}
}
private class M1 : Leetcode_40 {
private val res: MutableList<List<Int>> = ArrayList()
private val track = ArrayList<Int>()
private var sum = 0
override fun combinationSum2(candidates: IntArray, target: Int): List<List<Int>> {
candidates.sorted()
backtrack(candidates, target, 0)
return res
}
private fun backtrack(candidates: IntArray, target: Int, start: Int) : Boolean {
val toAdd = candidates[start]
val s = sum + toAdd
track.add(toAdd)
if (s == target) {
res.add(track)
return false
} else if (s > target) {
return false
}
for (i in start until candidates.size) {
val inRange = !backtrack(candidates, target, i)
track.removeLast()
if (!inRange) {
break
}
}
return true
}
}
}
// 200. Number of Islands
// https://leetcode.com/problems/number-of-islands/
private interface Leetcode_200 {
fun numIslands(grid: Array<CharArray>): Int
companion object : Testable {
override fun test() {
val tests = listOf(
arrayOf(
charArrayOf('1'),
charArrayOf('1'),
) to 1,
arrayOf(
charArrayOf('1','0','0','0','0'),
) to 1,
arrayOf(
charArrayOf('1','0','1','0','0'),
) to 2,
arrayOf(
charArrayOf('1','1','1','1','0'),
charArrayOf('1','1','0','1','0'),
charArrayOf('1','1','0','0','0'),
charArrayOf('0','0','0','0','0'),
) to 1,
arrayOf(
charArrayOf('1','1','0','0','0'),
charArrayOf('1','1','0','0','0'),
charArrayOf('0','0','1','0','0'),
charArrayOf('0','0','0','1','1'),
) to 3,
)
listOf(
M1()::numIslands,
M2()::numIslands,
).runTimedTests(tests) { a, b ->
invoke(a).assertEqualTo(b)
}
}
}
// UnionFind
private class M1 : Leetcode_200 {
private lateinit var uf : UnionFind
override fun numIslands(grid: Array<CharArray>): Int {
val n = grid.size
val m = grid[0].size
uf = UnionFind(m * n)
for (i in 0 until n) {
for (j in 0 until m) {
val index = i * m + j
val c = grid[i][j]
if (c != '1') continue
// top
if (i > 0) {
if (grid[i - 1][j] == '1') {
uf.union(index, (i - 1) * m + j)
}
}
// left
if (j > 0) {
if (grid[i][j-1] == '1') {
uf.union(index, index - 1)
}
}
}
}
val island = HashSet<Int>()
var counter = 0
for (i in 0 until n) {
for (j in 0 until m) {
val index = i * m + j
val c = grid[i][j]
if (c != '1') continue
if (!island.any { it != index && uf.connected(it, index) }) {
counter++
island.add(index)
}
}
}
return counter
}
}
private class M2 : Leetcode_200 {
override fun numIslands(grid: Array<CharArray>): Int {
val n = grid.size
val m = grid[0].size
var counter = 0
for (y in 0 until n) {
for (x in 0 until m) {
if (isLand(grid, x, y)) {
counter++
dfs(grid, x, y)
}
}
}
return counter
}
fun dfs(grid: Array<CharArray>, x: Int, y: Int) {
val n = grid.size
val m = grid[0].size
// out of bound
if (x < 0 || x >= m || y < 0 || y >= n) return
grid[y][x] = '0'
if (isLand(grid, x-1, y)) dfs(grid, x-1, y)
if (isLand(grid, x+1, y)) dfs(grid, x+1, y)
if (isLand(grid, x, y-1)) dfs(grid, x, y-1)
if (isLand(grid, x, y+1)) dfs(grid, x, y+1)
}
fun isLand(grid: Array<CharArray>, x: Int, y: Int) : Boolean {
val n = grid.size
val m = grid[0].size
// out of bound
if (x < 0 || x >= m || y < 0 || y >= n) return false
return grid[y][x] == '1'
}
}
}
val _20220804 = listOf<Testable>(
// Leetcode_40,
Leetcode_200,
)