Skip to content

Commit f5ce4e9

Browse files
More Problems
2 parents 8b356d2 + e0cbe0c commit f5ce4e9

23 files changed

+660
-1
lines changed

solutions/src/AdditiveNumber.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* https://leetcode.com/problems/additive-number/
3+
*/
4+
class AdditiveNumber {
5+
fun isAdditiveNumber(num: String): Boolean {
6+
val n = num.length
7+
for (i in 1..n / 2) {
8+
var j = 1
9+
while (maxOf(j, i) <= n - i - j) {
10+
if (isValid(i, j, num)) return true
11+
++j
12+
}
13+
}
14+
return false
15+
}
16+
17+
private fun isValid(i: Int, j: Int, num: String): Boolean {
18+
if (num[0] == '0' && i > 1) return false
19+
if (num[i] == '0' && j > 1) return false
20+
var sum: String
21+
var x1 = num.substring(0, i).toLong()
22+
var x2 = num.substring(i, i + j).toLong()
23+
var start = i + j
24+
while (start != num.length) {
25+
x2 += x1
26+
x1 = x2 - x1
27+
sum = x2.toString()
28+
if (!num.startsWith(sum, start)) return false
29+
start += sum.length
30+
}
31+
return true
32+
}
33+
}

solutions/src/BulbSwitcher.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import kotlin.math.sqrt
2+
3+
/**
4+
* https://leetcode.com/problems/bulb-switcher/
5+
*/
6+
class BulbSwitcher {
7+
fun bulbSwitch(n: Int) : Int {
8+
return sqrt(n.toDouble()).toInt()
9+
}
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*
2+
3+
4+
/**
5+
* https://leetcode.com/problems/combination-sum-iii/
6+
* cheated, for no reason because the answer was as complex as I was
7+
*/
8+
class CobinationSumThree {
9+
fun combinationSum3(k: Int, n: Int): List<List<Int>>? {
10+
val num = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
11+
val result: MutableList<List<Int>> = ArrayList()
12+
helper(result, ArrayList(), num, k, n, 0)
13+
return result
14+
}
15+
16+
private fun helper(result: MutableList<List<Int>>, list: MutableList<Int>, num: IntArray, k: Int, target: Int, start: Int) {
17+
if (k == 0 && target == 0) {
18+
result.add(ArrayList(list))
19+
} else {
20+
var i = start
21+
while (i < num.size && target > 0 && k > 0) {
22+
list.add(num[i])
23+
helper(result, list, num, k - 1, target - num[i], i + 1)
24+
list.removeAt(list.size - 1)
25+
i++
26+
}
27+
}
28+
}
29+
}

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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*
2+
3+
/**
4+
* https://leetcode.com/problems/find-k-pairs-with-smallest-sums/
5+
*/
6+
class FindKPairsWithSmallestSum {
7+
8+
fun kSmallestPairs(nums1: IntArray, nums2: IntArray, k: Int): List<List<Int>> {
9+
var pairSums: MutableList<Pair<Int, List<Int>>> = mutableListOf()
10+
val m = minOf(nums1.size, k)
11+
val n = minOf(nums2.size, k)
12+
for (i in 0 until m) {
13+
for (j in 0 until n) {
14+
pairSums.add(Pair(nums1[i] + nums2[j], listOf(nums1[i], nums2[j])))
15+
}
16+
}
17+
val seenSums = mutableSetOf<Int>()
18+
val returnSums = mutableListOf<List<Int>>()
19+
pairSums = pairSums.sortedBy { it.first }.toMutableList()
20+
return if (pairSums.size < k) pairSums.map { it.second } else pairSums.subList(0,k).map { it.second }
21+
}
22+
23+
}

solutions/src/FindPeakElement.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class FindPeakElement {
2+
fun findPeakElement(nums: IntArray): Int {
3+
if (nums.isEmpty()) {
4+
return -1
5+
}
6+
return binarySearchPeak(nums,0,nums.lastIndex)
7+
8+
}
9+
10+
private fun binarySearchPeak(arr: IntArray, lower: Int, upper: Int) : Int {
11+
if (upper == lower) {
12+
return lower
13+
}
14+
val mid = lower + (upper-lower)/2
15+
if (arr[mid] > arr[mid+1]) {
16+
return binarySearchPeak(arr,lower,mid)
17+
}
18+
else {
19+
return binarySearchPeak(arr,mid+1,upper)
20+
}
21+
}
22+
23+
}

solutions/src/HIndex.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*
2+
3+
/**
4+
* https://leetcode.com/problems/h-index/
5+
*/
6+
class HIndex {
7+
fun hIndex(citations: IntArray): Int {
8+
val citationsSize = citations.size
9+
val count = IntArray(citationsSize+1)
10+
citations.forEach {
11+
if (it > citationsSize) {
12+
count[citationsSize]++
13+
}
14+
else count[it]++
15+
}
16+
var total = 0
17+
for(i in citationsSize downTo 0) {
18+
total+=count[i]
19+
if (total >= i) {
20+
return i
21+
}
22+
}
23+
return 0
24+
}
25+
}

solutions/src/KthLargestElement.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Looked up solution
3+
*
4+
*/
5+
class KthLargestElement {
6+
fun findKthLargest(nums: IntArray, k: Int): Int {
7+
var k = k
8+
k = nums.size - k
9+
var lo = 0
10+
var hi = nums.size - 1
11+
while (lo < hi) {
12+
val j = partition(nums, lo, hi)
13+
if (j < k) {
14+
lo = j + 1
15+
} else if (j > k) {
16+
hi = j - 1
17+
} else {
18+
break
19+
}
20+
}
21+
return nums[k]
22+
}
23+
24+
private fun partition(a: IntArray, lo: Int, hi: Int): Int {
25+
var i = lo
26+
var j = hi + 1
27+
while (true) {
28+
while (i < hi && a[++i] < a[lo]);
29+
while (j > lo && a[lo] < a[--j]);
30+
if (i >= j) {
31+
break
32+
}
33+
exch(a, i, j)
34+
}
35+
exch(a, lo, j)
36+
return j
37+
}
38+
39+
private fun exch(a: IntArray, i: Int, j: Int) {
40+
val tmp = a[i]
41+
a[i] = a[j]
42+
a[j] = tmp
43+
}
44+
45+
}

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+
}

0 commit comments

Comments
 (0)