Skip to content

Commit

Permalink
[LeetCode] Add daily challenge (January 11).
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da authored Jan 11, 2021
1 parent c68483a commit 2b730b9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions solved/LeetCode/Challenges/2020/January/11.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
private val inf = 1000_000_000 + 100

fun merge(A: IntArray, m: Int, B: IntArray, n: Int): Unit {
for (i in m - 1 downTo 0) {
A[i + n] = A[i]
}
var idA = n
var idB = 0
for (i in A.indices) {
if (get(A, idA) < get(B, idB)) {
A[i] = A[idA]
idA++
} else {
A[i] = B[idB]
idB++
}
}
}

private fun get(arr: IntArray, idx: Int): Int {
if (idx >= arr.size) {
return inf
}
return arr[idx]
}

0 comments on commit 2b730b9

Please sign in to comment.