Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions combination-sum/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
val result = mutableListOf<List<Int>>()
val currentCombination = mutableListOf<Int>()

candidates.sort()

backtrack(candidates, target, 0, currentCombination, result)

return result
}

private fun backtrack(
candidates: IntArray,
remainingTarget: Int,
startIndex: Int,
currentCombination: MutableList<Int>,
result: MutableList<List<Int>>
) {
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)
}
}
}
27 changes: 27 additions & 0 deletions decode-ways/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -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]
}
}
13 changes: 13 additions & 0 deletions maximum-subarray/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions number-of-1-bits/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution {
fun hammingWeight(n: Int) = Integer.bitCount(n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코틀린에 깔쌈한 내장함수가있나보군요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 저도 이번에 처음 알았습니다 ㅎㅎ

}
9 changes: 9 additions & 0 deletions valid-palindrome/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깔끔하네용

}
}