Skip to content

Commit 85fe9a6

Browse files
better
1 parent 1dd34c7 commit 85fe9a6

File tree

7 files changed

+160
-3
lines changed

7 files changed

+160
-3
lines changed

solutions/src/CourseScheduleTwo.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* https://leetcode.com/problems/course-schedule-ii/
3+
*/
4+
class CourseScheduleTwo {
5+
fun findOrder(numCourses: Int, prerequisites: Array<IntArray>): IntArray {
6+
val courePrereqs = (0 until numCourses).map { Pair(it, mutableSetOf<Int>()) }.toMap().toMutableMap()
7+
val courseToListOfCoursesThatNeedIt = (0 until numCourses).map { Pair(it, mutableSetOf<Int>()) }.toMap().toMutableMap()
8+
prerequisites.forEach { desc ->
9+
val prereqList = mutableListOf<Int>()
10+
for(i in 1 until desc.size) {
11+
prereqList.add(desc[i])
12+
courseToListOfCoursesThatNeedIt.computeIfAbsent(desc[i]) { mutableSetOf()}.add(desc[0])
13+
}
14+
courePrereqs.computeIfAbsent(desc[0]) { mutableSetOf()}.addAll(prereqList)
15+
}
16+
val availableCourses = courePrereqs.filter { it.value.isEmpty() }.map { it.key }.toMutableList()
17+
availableCourses.forEach{
18+
courePrereqs.remove(it)
19+
}
20+
val courseSchedule = mutableListOf<Int>()
21+
val visited = availableCourses.toMutableSet()
22+
while (availableCourses.isNotEmpty()) {
23+
val current = availableCourses.removeAt(0)
24+
courseSchedule.add(current)
25+
val toQueue = mutableListOf<Int>()
26+
courseToListOfCoursesThatNeedIt.getOrDefault(current, mutableSetOf()).forEach {
27+
val preReqSet = courePrereqs[it]!!
28+
preReqSet.remove(current)
29+
if (preReqSet.isEmpty()) {
30+
courePrereqs.remove(it)
31+
toQueue.add(it)
32+
33+
}
34+
}
35+
availableCourses.addAll(toQueue)
36+
37+
}
38+
return if (courseSchedule.size != numCourses) intArrayOf() else courseSchedule.toIntArray()
39+
40+
41+
42+
}
43+
}

solutions/src/LargestNumber.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* https://leetcode.com/problems/largest-number/
3+
*/
4+
class LargestNumber {
5+
fun largestNumber(nums: IntArray) : String {
6+
if (nums.isEmpty()) {
7+
return ""
8+
}
9+
if (nums.all { it == 0 }) {
10+
return "0"
11+
}
12+
val strCompare = Comparator<String> { o1, o2 ->
13+
var s1 = o1+o2
14+
var s2 = o2+o1
15+
s2.compareTo(s1)
16+
}
17+
return nums.map { it.toString() } .sortedWith(strCompare).joinToString(separator = "")
18+
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
3+
*/
4+
class LowestCommonAncestorOfBinaryTree {
5+
fun lowestCommonAncestor(root: TreeNode?, p: TreeNode? , q: TreeNode?) : TreeNode? {
6+
return findAncestor(root!!,p!!.`val`,q!!.`val`)
7+
}
8+
9+
private fun findAncestor(node: TreeNode, t1: Int, t2: Int) : TreeNode {
10+
val t1ExistsLeft = binarySearch(node.left,t1)
11+
val t2ExistsLeft = binarySearch(node.left,t2)
12+
val t1ExistsRight = binarySearch(node.right,t1)
13+
val t2ExistsRight = binarySearch(node.right,t2)
14+
if (t1ExistsLeft && t2ExistsLeft) {
15+
return findAncestor(node.left!!,t1,t2)
16+
}
17+
else if (t2ExistsRight && t1ExistsRight) {
18+
return findAncestor(node.right!!,t1,t2)
19+
}
20+
else {
21+
return node
22+
}
23+
}
24+
private fun binarySearch(node: TreeNode?, target: Int) : Boolean {
25+
if (node == null) {
26+
return false
27+
}
28+
if (node.`val` == target) {
29+
return true
30+
}
31+
else {
32+
return binarySearch(node.left,target) || binarySearch(node.right,target)
33+
}
34+
}
35+
}

solutions/src/Power.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* https://leetcode.com/problems/powx-n/
3+
*/
4+
class Power {
5+
fun myPow(x: Double, n: Int): Double {
6+
if (n == 0) return 1.0
7+
if (n < 0) {
8+
return 1 / x * myPow(1 / x, -(n + 1))
9+
}
10+
return if (n % 2 == 0) myPow(x * x, n / 2) else x * myPow(x * x, n / 2)
11+
}
12+
}

solutions/src/SparseArrays.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* https://www.hackerrank.com/challenges/sparse-arrays/problem
3+
*/
4+
class SparseArrays {
5+
fun matchingStrings(strings: Array<String>, queries: Array<String>): Array<Int> {
6+
val stringsByOccurance = strings.groupingBy { it }.eachCount()
7+
return queries.map { stringsByOccurance[it] ?: 0 }.toTypedArray()
8+
9+
}
10+
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* https://leetcode.com/problems/sum-root-to-leaf-numbers/
3+
*/
4+
class SumRootToLeafNumbers {
5+
fun sumNumbers(root: TreeNode?) : Int {
6+
if (root == null) {
7+
return 0
8+
}
9+
val queue= mutableListOf(QueueItem(root,root.`val`.toString()))
10+
val finishedStrings = mutableListOf<String>()
11+
while (queue.isNotEmpty()) {
12+
val current = queue.removeAt(0)
13+
val currentNode = current.node
14+
val currentStr = current.str
15+
if (currentNode.left == null && currentNode.right == null) {
16+
finishedStrings.add(currentStr)
17+
}
18+
else {
19+
val toQueue = mutableListOf<QueueItem>()
20+
if (currentNode.left != null) {
21+
toQueue.add(QueueItem(currentNode.left!!,currentStr+currentNode.left!!.`val`))
22+
}
23+
if (currentNode.right != null) {
24+
toQueue.add(QueueItem(currentNode.right!!,currentStr+currentNode.right!!.`val`))
25+
}
26+
queue.addAll(toQueue)
27+
}
28+
29+
}
30+
return finishedStrings.map { it.toInt() }.sum()
31+
}
32+
33+
private class QueueItem(val node: TreeNode, val str: String)
34+
}

solutions/src/TestBed.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import kotlin.time.ExperimentalTime
12
import kotlin.time.measureTime
23

34

5+
@ExperimentalTime
46
fun main(args: Array<String>) {
57
println(measureTime {
6-
val tester = OutOfBoundsPaths()
8+
val tester = CourseScheduleTwo()
79
val results = listOf(
8-
tester.findPaths(1,3,3,0,1)
10+
tester.findOrder(2, arrayOf(intArrayOf(1,0)))
911

1012
)
11-
println(results.map { it })
13+
println(results.map { it.toList() })
1214
})
1315

1416
}

0 commit comments

Comments
 (0)