Adapt optimal C++/ JavaScript solution
This commit is contained in:
parent
18c1cccac1
commit
0f82896563
|
@ -0,0 +1,25 @@
|
|||
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 = nums.mapIndexed { index: Int, num: Int -> num to index }.toMap()
|
||||
println("numsToIndices = $numsToIndices")
|
||||
for ((index, num) in nums.withIndex()) {
|
||||
if (numsToIndices.containsValue(target - num)) {
|
||||
return intArrayOf(numsToIndices[target - num]!!, index)
|
||||
}
|
||||
}
|
||||
return intArrayOf(-1, -1)
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ import kotlin.test.Test
|
|||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ContainsDuplicateTest {
|
||||
class DuplicatesTest {
|
||||
@Test
|
||||
fun `it returns true for '1_2_3_1'`() {
|
||||
assertTrue(containsDuplicate(intArrayOf(1, 2, 3, 1)))
|
|
@ -0,0 +1,60 @@
|
|||
package space.dezentrale.prgrnd
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class SumOfTwoTest {
|
||||
@Test
|
||||
fun `simpleSolution returns an array with 0 and 1 for 2_7_11_15 and 9`() {
|
||||
val sumOfTwo = SumOfTwo()
|
||||
|
||||
val twoSum = sumOfTwo.simpleSolution(intArrayOf(2, 7, 11, 15), 9)
|
||||
|
||||
assertContentEquals(twoSum, intArrayOf(0, 1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `simpleSolution returns an array with 1 and 2 for 3_2_4 and 6`() {
|
||||
val sumOfTwo = SumOfTwo()
|
||||
|
||||
val twoSum = sumOfTwo.simpleSolution(intArrayOf(3, 2, 4), 6)
|
||||
|
||||
assertContentEquals(twoSum, intArrayOf(1, 2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `simpleSolution returns an array with 0 and 1 for 3_3 and 6`() {
|
||||
val sumOfTwo = SumOfTwo()
|
||||
|
||||
val twoSum = sumOfTwo.simpleSolution(intArrayOf(3, 3), 6)
|
||||
|
||||
assertContentEquals(twoSum, intArrayOf(0, 1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `optimalSolution returns an array with 0 and 1 for 2_7_11_15 and 9`() {
|
||||
val sumOfTwo = SumOfTwo()
|
||||
|
||||
val twoSum = sumOfTwo.optimalSolution(intArrayOf(2, 7, 11, 15), 9)
|
||||
|
||||
assertContentEquals(twoSum, intArrayOf(0, 1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `optimalSolution returns an array with 1 and 2 for 3_2_4 and 6`() {
|
||||
val sumOfTwo = SumOfTwo()
|
||||
|
||||
val twoSum = sumOfTwo.optimalSolution(intArrayOf(3, 2, 4), 6)
|
||||
|
||||
assertContentEquals(twoSum, intArrayOf(1, 2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `optimalSolution returns an array with 0 and 1 for 3_3 and 6`() {
|
||||
val sumOfTwo = SumOfTwo()
|
||||
|
||||
val twoSum = sumOfTwo.optimalSolution(intArrayOf(3, 3), 6)
|
||||
|
||||
assertContentEquals(twoSum, intArrayOf(0, 1))
|
||||
}
|
||||
}
|
Reference in New Issue