Skip to content

Commit ca4ceca

Browse files
committed
2 parents 16eeb47 + 52ad640 commit ca4ceca

File tree

5 files changed

+162
-35
lines changed

5 files changed

+162
-35
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
fun solve(b: Array<CharArray>): Unit {
3+
val rows = b.size
4+
val cols = b[0].size
5+
val dirs = intArrayOf(-1,0,1,0,-1)
6+
7+
val visit = HashSet<Pair<Int,Int>>() //mark NOT to be flipped
8+
fun dfs(r: Int, c: Int) {
9+
val coord = r to c
10+
if (r !in 0 until rows ||
11+
c !in 0 until cols ||
12+
coord in visit ||
13+
b[r][c] != 'O') return
14+
visit.add(coord)
15+
for (d in 0 until 4) {
16+
val nr = r + dirs[d]
17+
val nc = c + dirs[d+1]
18+
dfs(nr, nc)
19+
}
20+
}
21+
repeat(rows) { r ->
22+
val coordLeft = r to 0
23+
val coordRight = r to cols-1
24+
for (coord in listOf(coordLeft, coordRight)) {
25+
if (coord !in visit &&
26+
b[coord.first][coord.second] == 'O') {
27+
dfs(coord.first, coord.second)
28+
}
29+
}
30+
}
31+
repeat(cols) { c ->
32+
val coordTop = 0 to c
33+
val coordBottom = rows-1 to c
34+
for (coord in listOf(coordTop, coordBottom)) {
35+
if (coord !in visit &&
36+
b[coord.first][coord.second] == 'O') {
37+
dfs(coord.first, coord.second)
38+
}
39+
}
40+
}
41+
repeat(rows) { r ->
42+
repeat(cols) { c ->
43+
val coord = r to c
44+
if (coord !in visit && b[r][c] == 'O') b[r][c] = 'X'
45+
}
46+
}
47+
}
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
fun findRedundantConnection(edges: Array<IntArray>): IntArray {
3+
val parent = IntArray(edges.size + 1) {it}
4+
fun union(x: Int, y: Int) {
5+
parent[x] = y
6+
}
7+
fun find(x: Int): Int {
8+
if (x == parent[x]) return x
9+
parent[x] = find(parent[x])
10+
return parent[x]
11+
}
12+
13+
for (edge in edges) {
14+
val v1 = edge[0]
15+
val v2 = edge[1]
16+
val p1 = find(v1)
17+
val p2 = find(v2)
18+
if (p1 == p2) return intArrayOf(v1, v2)
19+
union(p1, p2)
20+
}
21+
return intArrayOf()
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
fun maxAreaOfIsland(g: Array<IntArray>): Int {
3+
val rows = g.size
4+
val cols = g[0].size
5+
val dirs = intArrayOf(-1,0,1,0,-1)
6+
7+
var res = 0
8+
val visit = HashSet<Pair<Int,Int>>()
9+
val q = LinkedList<Pair<Int,Int>>()
10+
fun count(): Int {
11+
var res = 0
12+
while (q.isNotEmpty()) {
13+
res++
14+
val coord = q.poll()
15+
for (d in 0 until 4) {
16+
val nr = coord.first + dirs[d]
17+
val nc = coord.second + dirs[d+1]
18+
val newCoord = nr to nc
19+
if (nr in 0 until rows &&
20+
nc in 0 until cols &&
21+
newCoord !in visit &&
22+
g[nr][nc] == 1) {
23+
q.add(newCoord)
24+
visit.add(newCoord)
25+
}
26+
}
27+
}
28+
return res
29+
}
30+
31+
repeat(rows) { r ->
32+
repeat(cols) { c ->
33+
val coord = r to c
34+
if (coord !in visit &&
35+
g[r][c] == 1) {
36+
q.add(coord)
37+
visit.add(coord)
38+
res = maxOf(res, count())
39+
}
40+
}
41+
}
42+
return res
43+
}
44+
}

LeetCode/721. Accounts Merge_union_find.kt

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
class Solution {
2-
val parents = HashMap<String, String>() //child email: parent email
3-
42
fun accountsMerge(accs: List<List<String>>): List<List<String>> {
5-
val owner = HashMap<String, String>() //email: owner
6-
val unions = HashMap<String, TreeSet<String>>() //parent email: set of children email, TreeSet will sort elements automatically
3+
val parents = HashMap<String, String>() //child email: parent email
4+
val owners = HashMap<String, String>() //email: owner
5+
val unions = HashMap<String, TreeSet<String>>().withDefault {TreeSet()} //parent email : child emails, TreeSet will sort children automatically
76
val res = mutableListOf<List<String>>()
87

9-
accs.forEach { a ->
10-
for (i in 1 until a.size) {
11-
parents[a[i]] = a[i] //set each email to be its own parent, including the parent itself
12-
owner[a[i]] = a[0] //set owner of each email
8+
fun findParent(email: String): String =
9+
if (parents[email] === email) email else findParent(parents[email]!!)
10+
11+
accs.forEach { acc ->
12+
val owner = acc[0]
13+
for (i in 1 until acc.size) {
14+
val email = acc[i]
15+
parents[email] = email //set each email to be its own parent, including the parent itself
16+
owners[email] = owner //set owner of each email
1317
}
1418
}
15-
accs.forEach { a ->
16-
val p = find(a[1])
17-
for (i in 2 until a.size) parents[find(a[i])] = p //set other emails in the account to have the first email in the account as parent email
19+
accs.forEach { acc ->
20+
val parent = findParent(acc[1])
21+
for (i in 2 until acc.size) {
22+
val email = acc[i]
23+
parents[findParent(email)] = parent
24+
} //set other emails in the account to have the first email in the account as parent email
1825
}
19-
accs.forEach { a ->
20-
val p = find(a[1])
21-
if (p !in unions) unions[p] = TreeSet() //new a set if the parent does not exist in the map
22-
for (i in 1 until a.size) unions[p]!!.add(a[i]) //assign emails with the same parent to 1 set
26+
accs.forEach { acc ->
27+
val parent = findParent(acc[1])
28+
for (i in 1 until acc.size) {
29+
unions[parent] = unions.getValue(parent).apply {add(acc[i])}
30+
} //assign emails with the same parent to 1 set
2331
}
24-
for (p in unions.keys) {
25-
val emails = LinkedList(unions[p]!!).apply { push(owner[p]) } //new a list with emails in it, then push the owner to start of it
26-
res.add(emails) //add to result
32+
unions.keys.forEach { parent ->
33+
val emails = LinkedList(unions[parent]!!).apply {push(owners[parent])} //new a list with emails in it, then push the owner to start of it
34+
res.add(emails)
2735
}
2836
return res
2937
}
30-
31-
fun find(email: String): String = // find parent email of this email
32-
if (parents[email] === email) email else find(parents[email]!!)
3338
}
3439

3540
/*

LeetCode/75. Sort Colors.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
class Solution {
2+
val RED = 0
3+
val WHITE = 1
4+
val BLUE = 2
5+
26
fun sortColors(nums: IntArray) {
37
var red = 0
48
var white = 0
59
var blue = nums.size - 1
10+
11+
fun swap(a: Int, b: Int) {
12+
val tmp = nums[a]
13+
nums[a] = nums[b]
14+
nums[b] = tmp
15+
}
616

717
while(white <= blue) {
8-
if (nums[white] == 0) { //when white pointer is pointing to red element
9-
val tmp = nums[red]
10-
nums[red] = nums[white]
11-
nums[white] = tmp //swap red pointer element with white pointer element
12-
13-
white++
14-
red++ //the pointed element is in correct position, increment both what they are pointing to by 1
15-
} else if (nums[white] == 1) { //if white pointer is pointing to white element
16-
white++ //increment white pointer by 1
17-
} else { //when white pointer is pointing to blue element
18-
val tmp = nums[white]
19-
nums[white] = nums[blue]
20-
nums[blue] = tmp //swap white pointer element with blue pointer element
21-
blue-- //subtract by 1 as the previous element does not need moving anymore
18+
when (nums[white]) {
19+
RED -> {
20+
swap(white, red)
21+
red++
22+
white++ //the pointed element is in correct position, increment both what they are pointing to by 1
23+
}
24+
WHITE -> white++
25+
BLUE -> {
26+
swap(white, blue)
27+
blue-- //subtract by 1 as the previous element does not need moving anymore
28+
}
2229
}
2330
}
2431
}

0 commit comments

Comments
 (0)