diff --git a/combination-sum/hyunjung-choi.kt b/combination-sum/hyunjung-choi.kt new file mode 100644 index 000000000..615dc915b --- /dev/null +++ b/combination-sum/hyunjung-choi.kt @@ -0,0 +1,43 @@ +class Solution { + fun combinationSum(candidates: IntArray, target: Int): List> { + val result = mutableListOf>() + val currentCombination = mutableListOf() + + candidates.sort() + + backtrack(candidates, target, 0, currentCombination, result) + + return result + } + + private fun backtrack( + candidates: IntArray, + remainingTarget: Int, + startIndex: Int, + currentCombination: MutableList, + result: MutableList> + ) { + if (remainingTarget == 0) { + result.add(currentCombination.toList()) + return + } + + if (remainingTarget < 0) { + return + } + + for (i in startIndex until candidates.size) { + val candidate = candidates[i] + + if (candidate > remainingTarget) { + break + } + + currentCombination.add(candidate) + + backtrack(candidates, remainingTarget - candidate, i, currentCombination, result) + + currentCombination.removeAt(currentCombination.size - 1) + } + } +} diff --git a/decode-ways/hyunjung-choi.kt b/decode-ways/hyunjung-choi.kt new file mode 100644 index 000000000..f77be775b --- /dev/null +++ b/decode-ways/hyunjung-choi.kt @@ -0,0 +1,27 @@ +class Solution { + fun numDecodings(s: String): Int { + if (s.isEmpty() || s[0] == '0') return 0 + + val n = s.length + val dp = IntArray(n + 1) + + dp[0] = 1 + dp[1] = 1 + + for (i in 2..n) { + val currentChar = s[i - 1] + val prevChar = s[i - 2] + + if (currentChar != '0') { + dp[i] += dp[i - 1] + } + + val twoDigit = (prevChar - '0') * 10 + (currentChar - '0') + if (twoDigit in 10..26) { + dp[i] += dp[i - 2] + } + } + + return dp[n] + } +} diff --git a/maximum-subarray/hyunjung-choi.kt b/maximum-subarray/hyunjung-choi.kt new file mode 100644 index 000000000..d859d1014 --- /dev/null +++ b/maximum-subarray/hyunjung-choi.kt @@ -0,0 +1,13 @@ +class Solution { + fun maxSubArray(nums: IntArray): Int { + var currentSum = nums[0] + var maxSum = nums[0] + + for (i in 1 until nums.size) { + currentSum = if (currentSum < 0) nums[i] else currentSum + nums[i] + maxSum = maxOf(currentSum, maxSum) + } + + return maxSum + } +} diff --git a/number-of-1-bits/hyunjung-choi.kt b/number-of-1-bits/hyunjung-choi.kt new file mode 100644 index 000000000..d16916f1c --- /dev/null +++ b/number-of-1-bits/hyunjung-choi.kt @@ -0,0 +1,3 @@ +class Solution { + fun hammingWeight(n: Int) = Integer.bitCount(n) +} diff --git a/valid-palindrome/hyunjung-choi.kt b/valid-palindrome/hyunjung-choi.kt new file mode 100644 index 000000000..d96360468 --- /dev/null +++ b/valid-palindrome/hyunjung-choi.kt @@ -0,0 +1,9 @@ +import java.util.Locale.getDefault + +class Solution { + fun isPalindrome(s: String): Boolean { + if (s.isBlank()) return true + val cleanedString = s.trim().filter { it.isLetterOrDigit() }.lowercase(getDefault()) + return cleanedString == cleanedString.reversed() + } +}