From 371b995cd398f74bac19223d809fdd4e0fdb0317 Mon Sep 17 00:00:00 2001 From: namjackson Date: Sat, 26 Sep 2020 01:19:20 +0900 Subject: [PATCH 1/3] feat(TapeEquilibrium): Create TapeEquilibrium --- namjackson/TapeEquilibrium.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 namjackson/TapeEquilibrium.kt diff --git a/namjackson/TapeEquilibrium.kt b/namjackson/TapeEquilibrium.kt new file mode 100644 index 0000000..770ca26 --- /dev/null +++ b/namjackson/TapeEquilibrium.kt @@ -0,0 +1,13 @@ +import kotlin.math.abs + +fun solution(A: IntArray): Int { + var minimum = Int.MAX_VALUE + var firstPart = 0 + var secondPart = A.sum() + (0 until A.lastIndex).forEach { + firstPart += A[it] + secondPart -= A[it] + minimum = abs(firstPart - secondPart).takeIf { it < minimum } ?: minimum + } + return minimum +} \ No newline at end of file From 7400535226ae450959dd33fd3c6848471ec2c7a7 Mon Sep 17 00:00:00 2001 From: namjackson Date: Sat, 10 Oct 2020 21:09:33 +0900 Subject: [PATCH 2/3] feat(TargetNumber): Create TargetNumber --- namjackson/TargetNumber.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 namjackson/TargetNumber.kt diff --git a/namjackson/TargetNumber.kt b/namjackson/TargetNumber.kt new file mode 100644 index 0000000..37269b4 --- /dev/null +++ b/namjackson/TargetNumber.kt @@ -0,0 +1,16 @@ +class Solution { + fun solution(numbers: IntArray, target: Int): Int { + return search(numbers, target, 0, 0) + } + + private fun search(numbers: IntArray, target: Int, index: Int, sum: Int): Int { + return when { + index < numbers.size -> { + val plusCount = search(numbers, target, index + 1, sum + numbers[index]) + val minusCount = search(numbers, target, index + 1, sum - numbers[index]) + plusCount + minusCount + } + else -> if (sum == target) 1 else 0 + } + } +} From 57b6e525bce1bae9041d8280a070f5cab068ebec Mon Sep 17 00:00:00 2001 From: namjackson Date: Mon, 12 Oct 2020 03:20:10 +0900 Subject: [PATCH 3/3] feat(CombinationSum): Create CombinationSum's Solution --- namjackson/CombinationSum.kt | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 namjackson/CombinationSum.kt diff --git a/namjackson/CombinationSum.kt b/namjackson/CombinationSum.kt new file mode 100644 index 0000000..6f08b94 --- /dev/null +++ b/namjackson/CombinationSum.kt @@ -0,0 +1,29 @@ +class Solution { + fun combinationSum(candidates: IntArray, target: Int): List> { + val resultList = mutableListOf>() + search(candidates.sortedArrayDescending(), target, intArrayOf(), resultList) + return resultList.distinct() + } + + private fun search( + candidates: IntArray, + target: Int, + sumArray: IntArray, + resultList: MutableList> + ) { + val sum = sumArray.sum() + candidates.forEach { num -> + val gap = target - (sum + num) + when { + (gap == 0) -> { + val list = sumArray.toMutableList().also { it.add(num) } + resultList.add(list.sorted()) + } + (gap > 0) -> { + val list = sumArray.toMutableList().also { it.add(num) } + search(candidates, target, list.toIntArray(), resultList) + } + } + } + } +} \ No newline at end of file