Skip to content

Commit 41e7344

Browse files
I think i'm cheating myself too much
1 parent 85fe9a6 commit 41e7344

File tree

6 files changed

+199
-3
lines changed

6 files changed

+199
-3
lines changed
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/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+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import kotlin.math.min
2+
3+
/**
4+
* https://leetcode.com/problems/minimum-size-subarray-sum/
5+
*/
6+
class MiniumSizeSubArraySum {
7+
fun minSubArrayLen(s: Int, nums: IntArray) : Int {
8+
if (nums.isEmpty()) {
9+
return 0
10+
}
11+
var minLength : Int? = null
12+
var lower = 0
13+
var upper = 0
14+
var sumAtLower = IntArray(nums.size)
15+
var sumUpper = IntArray(nums.size)
16+
val totalSum = nums.sum()
17+
for(i in nums.indices) {
18+
if (i == 0) {
19+
sumAtLower[0] = nums[0]
20+
}
21+
else {
22+
sumAtLower[i]=nums[i]+sumAtLower[i-1]
23+
}
24+
}
25+
for (i in nums.lastIndex downTo 0) {
26+
if (i == nums.lastIndex) {
27+
sumUpper[i] = nums[i]
28+
}
29+
else {
30+
sumUpper[i] = nums[i]+sumUpper[i+1]
31+
}
32+
}
33+
while (lower in 0..upper && upper < nums.size) {
34+
val sum = when {
35+
lower == 0 -> {
36+
sumAtLower[upper]
37+
}
38+
upper == nums.lastIndex -> {
39+
sumUpper[lower]
40+
}
41+
else -> {
42+
totalSum - (sumAtLower[lower-1]+sumUpper[upper+1])
43+
}
44+
}
45+
if (sum >= s) {
46+
minLength = minOf(1 + (upper- lower), minLength ?: Int.MAX_VALUE)
47+
lower++
48+
}
49+
else {
50+
upper++
51+
}
52+
}
53+
return minLength ?: 0
54+
}
55+
}

solutions/src/Search2DMatrixTwo.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* https://leetcode.com/problems/search-a-2d-matrix-ii/
3+
*/
4+
class Search2DMatrixTwo {
5+
fun searchMatrix(matrix: Array<IntArray>, target: Int): Boolean {
6+
if (matrix.isEmpty()) {
7+
return false
8+
}
9+
if (matrix[0].isEmpty()) {
10+
return false
11+
}
12+
for (i in matrix.indices) {
13+
if (matrix[i].binarySearch(target) >= 0) {
14+
return true
15+
}
16+
}
17+
for (j in matrix[0].indices) {
18+
if (binarySearchColumn(matrix,j,0,matrix.lastIndex,target)) {
19+
return true
20+
}
21+
}
22+
return false
23+
}
24+
25+
private fun binarySearchColumn(matrix: Array<IntArray>, column: Int, lower: Int, upper: Int, target: Int) : Boolean {
26+
return if (lower > upper) {
27+
false
28+
}
29+
else {
30+
val mid = lower + (upper-lower)/2
31+
when {
32+
matrix[mid][column] == target -> {
33+
true
34+
}
35+
matrix[mid][column] > target -> {
36+
binarySearchColumn(matrix,column,lower,mid-1,target)
37+
}
38+
else -> {
39+
binarySearchColumn(matrix,column,mid+1,upper,target)
40+
}
41+
}
42+
}
43+
}
44+
}

solutions/src/SingleNumberIII.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* https://leetcode.com/problems/single-number-iii/
3+
*/
4+
class SingleNumberIII {
5+
fun singleNumber(nums: IntArray) : IntArray {
6+
return nums
7+
.toList()
8+
.groupingBy { it }
9+
.eachCount()
10+
.filter { it.value == 1 }
11+
.map { it.key }
12+
.toIntArray()
13+
}
14+
}

solutions/src/TestBed.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@ import kotlin.time.measureTime
55
@ExperimentalTime
66
fun main(args: Array<String>) {
77
println(measureTime {
8-
val tester = CourseScheduleTwo()
8+
val tester = Search2DMatrixTwo()
99
val results = listOf(
10-
tester.findOrder(2, arrayOf(intArrayOf(1,0)))
10+
tester.searchMatrix(
11+
arrayOf(
12+
intArrayOf(1,4,7,11,15),
13+
intArrayOf(2,5,8,12,19),
14+
intArrayOf(3,6,9,16,22),
15+
intArrayOf(10,13,14,17,24),
16+
intArrayOf(18,21,23,26,30)
17+
),
18+
20
19+
)
1120

1221
)
13-
println(results.map { it.toList() })
22+
println(results.map { it })
1423
})
1524

1625
}

0 commit comments

Comments
 (0)