26 lines
808 B
Kotlin
26 lines
808 B
Kotlin
package space.dezentrale.prgrnd
|
|
|
|
class SumOfTwo {
|
|
fun simpleSolution(nums: IntArray, target: Int): IntArray {
|
|
nums.forEachIndexed { idxA, numA ->
|
|
nums.forEachIndexed { idxB, numB ->
|
|
if (idxA != idxB && numA + numB == target) {
|
|
return intArrayOf(idxA, idxB)
|
|
}
|
|
}
|
|
}
|
|
return intArrayOf(-1, -1)
|
|
}
|
|
|
|
fun optimalSolution(nums: IntArray, target: Int): IntArray {
|
|
val numsToIndices = mutableMapOf<Int, Int>()
|
|
for ((index, num) in nums.withIndex()) {
|
|
if (numsToIndices.containsValue(target - num)) {
|
|
return intArrayOf(numsToIndices[target - num]!!, index)
|
|
}
|
|
numsToIndices[target] = index
|
|
}
|
|
return intArrayOf(-1, -1)
|
|
}
|
|
}
|