Skip to content

Commit 03c9d85

Browse files
Merge pull request #157
add new problems 22.02
2 parents 32f5c00 + 96c5a03 commit 03c9d85

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

.idea/other.xml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.heap.maxSubsequence
4+
import com.github.contest.binaryTree.recoverPreorderTraversal
55
import java.util.TreeMap
66

77

@@ -11,7 +11,7 @@ import java.util.TreeMap
1111

1212
fun main() {
1313

14-
maxSubsequence(intArrayOf(-1, -2, 3, 4), 3).apply { this.printArray() }
14+
recoverPreorderTraversal("1-2--3--4-5--6--7")
1515
}
1616

1717
fun generateTesting() {

contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,38 @@ class FindElements(private val root: TreeNode?) {
3333
return false
3434
}
3535

36-
}
36+
}
37+
38+
/**
39+
* 1028. Recover a Tree From Preorder Traversal
40+
*/
41+
42+
fun recoverPreorderTraversal(traversal: String): TreeNode? {
43+
val path = mutableListOf<TreeNode>()
44+
var pos = 0
45+
while (pos < traversal.length) {
46+
var level = 0
47+
while (traversal[pos] == '-') {
48+
level++
49+
pos++
50+
}
51+
var value = 0
52+
while (pos < traversal.length && traversal[pos].isDigit()) {
53+
value = value * 10 + (traversal[pos] - '0')
54+
pos++
55+
}
56+
val node = TreeNode(value)
57+
if (level == path.size) {
58+
if (path.isNotEmpty()) {
59+
path.last().left = node
60+
}
61+
} else {
62+
while (level < path.size) {
63+
path.removeLast()
64+
}
65+
path.last().right = node
66+
}
67+
path.add(node)
68+
}
69+
return path.firstOrNull()
70+
}

contest/src/main/java/com/github/contest/graph/GraphLeetcode.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.contest.graph
22

3+
import java.util.LinkedList
4+
35
/**
46
* 997. Find the Town Judge
57
*/
@@ -16,4 +18,50 @@ fun findJudge(n: Int, trust: Array<IntArray>): Int {
1618
if (inEdges[person] == n - 1) return person
1719
}
1820
return -1
21+
}
22+
23+
/**
24+
* 1971. Find if Path Exists in Graph
25+
*/
26+
27+
fun validPath(n: Int, edges: Array<IntArray>, source: Int, destination: Int): Boolean {
28+
if (source == destination) return true
29+
val adj = Array(n) { mutableListOf<Int>() }
30+
31+
for (edge in edges) {
32+
adj[edge[0]].add(edge[1])
33+
adj[edge[1]].add(edge[0])
34+
}
35+
val visited = BooleanArray(n) { false }
36+
val queue = LinkedList<Int>()
37+
queue.offer(source)
38+
visited[source] = true
39+
40+
while (queue.isNotEmpty()) {
41+
val vertex = queue.poll()
42+
if (vertex == destination) return true
43+
44+
for (v in adj[vertex]) {
45+
if (!visited[v]) {
46+
visited[v] = true
47+
queue.offer(v)
48+
}
49+
}
50+
}
51+
52+
return false
53+
}
54+
55+
/**
56+
* 1791. Find Center of Star Graph
57+
*/
58+
59+
fun findCenter(edges: Array<IntArray>): Int {
60+
val (a, b) = edges[0]
61+
val (c, d) = edges[1]
62+
63+
return when {
64+
a == c || a == d -> a
65+
else -> b
66+
}
1967
}

0 commit comments

Comments
 (0)