Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30343b1

Browse files
topological sorting
1 parent 2d69bea commit 30343b1

File tree

5 files changed

+250
-0
lines changed

5 files changed

+250
-0
lines changed

src/com/blankj/hard/_329/Solution.kt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.blankj.hard._329
2+
3+
import com.blankj.ext.print
4+
import com.blankj.structure.MultiDimensionArray
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun longestIncreasingPath(matrix: Array<IntArray>): Int {
9+
if (matrix.isEmpty() || matrix[0].isEmpty()) return 0
10+
val lengths = Array(matrix.size) { IntArray(matrix[0].size) }
11+
var res = Int.MIN_VALUE
12+
for (i in matrix.indices) {
13+
for (j in matrix[0].indices) {
14+
val length = dfs(matrix, i, j, lengths)
15+
res = max(res, length)
16+
}
17+
}
18+
return res
19+
}
20+
21+
private fun dfs(
22+
matrix: Array<IntArray>,
23+
i: Int, j: Int,
24+
lengths: Array<IntArray>
25+
): Int {
26+
if (lengths[i][j] > 0) return lengths[i][j]
27+
val dirs = arrayOf(intArrayOf(0, 1), intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, -1))
28+
var length = 1
29+
for (dir in dirs) {
30+
val row = i + dir[0]
31+
val col = j + dir[1]
32+
if (row in matrix.indices && col in matrix[0].indices
33+
&& matrix[row][col] > matrix[i][j]) {
34+
val path = dfs(matrix, row, col, lengths)
35+
length = max(path + 1, length)
36+
}
37+
}
38+
lengths[i][j] = length
39+
return lengths[i][j]
40+
}
41+
}
42+
43+
fun main() {
44+
Solution().longestIncreasingPath(
45+
MultiDimensionArray.createTestData(
46+
"[[1]]"
47+
)
48+
).print()
49+
Solution().longestIncreasingPath(
50+
arrayOf(
51+
intArrayOf(9,9,4),
52+
intArrayOf(6,6,8),
53+
intArrayOf(2,1,1),
54+
)
55+
).print()
56+
Solution().longestIncreasingPath(
57+
MultiDimensionArray.createTestData(
58+
"[[3,4,5],[3,2,6],[2,2,1]]"
59+
)
60+
).print()
61+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.blankj.medium._207
2+
3+
import com.blankj.ext.print
4+
import com.blankj.structure.MultiDimensionArray
5+
6+
class Solution {
7+
fun canFinish(numCourses: Int, prerequisites: Array<IntArray>): Boolean {
8+
if (numCourses <= 0) return false
9+
val inDegree = IntArray(numCourses)
10+
for (prerequisite in prerequisites) {
11+
inDegree[prerequisite[0]]++
12+
}
13+
val queue = ArrayDeque<Int>()
14+
queue.addAll(inDegree.indices.filter { inDegree[it] == 0 })
15+
var res = 0
16+
while (queue.isNotEmpty()) {
17+
res++
18+
val curElement = queue.removeFirst()
19+
for (prerequisite in prerequisites) {
20+
if (prerequisite[1] == curElement) {
21+
inDegree[prerequisite[0]]--
22+
if (inDegree[prerequisite[0]] == 0) queue.add(prerequisite[0])
23+
}
24+
}
25+
}
26+
return res == numCourses
27+
}
28+
}
29+
30+
fun main() {
31+
Solution().canFinish(
32+
2, arrayOf(
33+
intArrayOf(1, 0)
34+
)
35+
).print()
36+
Solution().canFinish(
37+
4, arrayOf(
38+
intArrayOf(1, 0),
39+
intArrayOf(2, 0),
40+
intArrayOf(3, 1),
41+
intArrayOf(3, 2)
42+
)
43+
).print()
44+
Solution().canFinish(
45+
1, arrayOf(
46+
)
47+
).print()
48+
Solution().canFinish(
49+
2,
50+
MultiDimensionArray.createTestData("[[1,0],[0,1]]")
51+
).print()
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.blankj.medium._269
2+
3+
import com.blankj.ext.print
4+
5+
6+
class Solution {
7+
fun alienOrder(words: Array<String>): String {
8+
if (words.isEmpty()) return ""
9+
// graph stores outDegree's chars(value) of char(key)
10+
val graph = mutableMapOf<Char, MutableSet<Char>>()
11+
val inDegrees = mutableMapOf<Char, Int>()
12+
for (word in words) {
13+
for (c in word.toCharArray()) {
14+
graph.putIfAbsent(c, mutableSetOf())
15+
inDegrees.putIfAbsent(c, 0)
16+
}
17+
}
18+
for (i in 1 until words.size) {
19+
val w1 = words[i - 1]
20+
val w2 = words[i]
21+
if (w1.startsWith(w2) && w1 != w2) return ""
22+
var j = 0
23+
while (j < w1.length && j < w2.length) {
24+
val c1 = w1[j]
25+
val c2 = w2[j]
26+
if (c1 != c2) {
27+
if (!graph[c1]!!.contains(c2)) {
28+
graph[c1]!!.add(c2)
29+
inDegrees[c2] = inDegrees[c2]!! + 1
30+
}
31+
break
32+
}
33+
j++
34+
}
35+
}
36+
val queue = ArrayDeque<Char>()
37+
queue.addAll(inDegrees.keys.filter { inDegrees[it] == 0 })
38+
val builder = StringBuilder()
39+
while (queue.isNotEmpty()) {
40+
val ch = queue.removeFirst()
41+
builder.append(ch)
42+
for (neighbor in graph[ch]!!) {
43+
inDegrees[neighbor] = inDegrees[neighbor]!! - 1
44+
if (inDegrees[neighbor] == 0) {
45+
queue.add(neighbor)
46+
}
47+
}
48+
}
49+
return if (builder.length == graph.size) builder.toString() else ""
50+
}
51+
}
52+
53+
54+
fun main() {
55+
Solution().alienOrder(arrayOf("wrt","wrf","er","ett","rftt")).print()
56+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.blankj.medium._444
2+
3+
import com.blankj.ext.print
4+
5+
class Solution {
6+
fun sequenceReconstruction(nums: IntArray, sequences: List<List<Int>>): Boolean {
7+
if (nums.isEmpty() && sequences.isEmpty()) return true
8+
if (nums.isEmpty() || sequences.isEmpty()) return false
9+
val graph = mutableMapOf<Int, MutableSet<Int>>()
10+
val inDegrees = mutableMapOf<Int, Int>()
11+
for (seq in sequences) {
12+
for (num in seq) {
13+
if (num < 1 || num > nums.size) {
14+
return false
15+
}
16+
graph.putIfAbsent(num, mutableSetOf())
17+
inDegrees.putIfAbsent(num, 0)
18+
}
19+
constructDirectedGraph(seq, graph, inDegrees)
20+
}
21+
val queue = ArrayDeque<Int>()
22+
queue.addAll(inDegrees.keys.filter { inDegrees[it] == 0 })
23+
val res = mutableListOf<Int>()
24+
while (queue.size == 1) {
25+
val num = queue.removeFirst()
26+
res.add(num)
27+
for (next in graph.getValue(num)) {
28+
inDegrees[next] = inDegrees[next]!! - 1
29+
if (inDegrees[next] == 0) {
30+
queue.add(next)
31+
}
32+
}
33+
}
34+
return res.toIntArray().contentEquals(nums)
35+
}
36+
37+
private fun constructDirectedGraph(
38+
seq: List<Int>,
39+
graph: MutableMap<Int, MutableSet<Int>>,
40+
inDegrees: MutableMap<Int, Int>
41+
) {
42+
for (i in 1 until seq.size) {
43+
val num1 = seq[i - 1]
44+
val num2 = seq[i]
45+
if (!graph.getValue(num1).contains(num2)) {
46+
graph.getValue(num1).add(num2)
47+
inDegrees[num2] = inDegrees[num2]!! + 1
48+
}
49+
}
50+
}
51+
}
52+
53+
fun main() {
54+
Solution().sequenceReconstruction(
55+
intArrayOf(1, 2, 3),
56+
listOf(
57+
listOf(1, 2),
58+
listOf(1,3),
59+
)
60+
).print()
61+
Solution().sequenceReconstruction(
62+
intArrayOf(1, 2, 3),
63+
listOf(
64+
listOf(1, 2),
65+
listOf(1,3),
66+
listOf(2,3),
67+
)
68+
).print()
69+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.blankj.structure
2+
3+
object MultiDimensionArray {
4+
public fun createTestData(data: String): Array<IntArray> {
5+
val rows = data.trim().substring(2, data.length - 2) // Remove outer brackets
6+
val rowStrings = rows.split("],[").toTypedArray() // Split by "],["
7+
val result = Array(rowStrings.size) {
8+
rowStrings[it].split(",").map { it.toInt() }.toIntArray() // Convert to integers
9+
}
10+
return result
11+
}
12+
}

0 commit comments

Comments
 (0)