Skip to content

Commit 148f92e

Browse files
Merge pull request #1780 from hyunjung-choi/main
[hyunjung-choi] WEEK 03 solutions
2 parents 027598f + abd7113 commit 148f92e

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

combination-sum/hyunjung-choi.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
3+
val result = mutableListOf<List<Int>>()
4+
val currentCombination = mutableListOf<Int>()
5+
6+
candidates.sort()
7+
8+
backtrack(candidates, target, 0, currentCombination, result)
9+
10+
return result
11+
}
12+
13+
private fun backtrack(
14+
candidates: IntArray,
15+
remainingTarget: Int,
16+
startIndex: Int,
17+
currentCombination: MutableList<Int>,
18+
result: MutableList<List<Int>>
19+
) {
20+
if (remainingTarget == 0) {
21+
result.add(currentCombination.toList())
22+
return
23+
}
24+
25+
if (remainingTarget < 0) {
26+
return
27+
}
28+
29+
for (i in startIndex until candidates.size) {
30+
val candidate = candidates[i]
31+
32+
if (candidate > remainingTarget) {
33+
break
34+
}
35+
36+
currentCombination.add(candidate)
37+
38+
backtrack(candidates, remainingTarget - candidate, i, currentCombination, result)
39+
40+
currentCombination.removeAt(currentCombination.size - 1)
41+
}
42+
}
43+
}

decode-ways/hyunjung-choi.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
fun numDecodings(s: String): Int {
3+
if (s.isEmpty() || s[0] == '0') return 0
4+
5+
val n = s.length
6+
val dp = IntArray(n + 1)
7+
8+
dp[0] = 1
9+
dp[1] = 1
10+
11+
for (i in 2..n) {
12+
val currentChar = s[i - 1]
13+
val prevChar = s[i - 2]
14+
15+
if (currentChar != '0') {
16+
dp[i] += dp[i - 1]
17+
}
18+
19+
val twoDigit = (prevChar - '0') * 10 + (currentChar - '0')
20+
if (twoDigit in 10..26) {
21+
dp[i] += dp[i - 2]
22+
}
23+
}
24+
25+
return dp[n]
26+
}
27+
}

maximum-subarray/hyunjung-choi.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
fun maxSubArray(nums: IntArray): Int {
3+
var currentSum = nums[0]
4+
var maxSum = nums[0]
5+
6+
for (i in 1 until nums.size) {
7+
currentSum = if (currentSum < 0) nums[i] else currentSum + nums[i]
8+
maxSum = maxOf(currentSum, maxSum)
9+
}
10+
11+
return maxSum
12+
}
13+
}

number-of-1-bits/hyunjung-choi.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution {
2+
fun hammingWeight(n: Int) = Integer.bitCount(n)
3+
}

valid-palindrome/hyunjung-choi.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java.util.Locale.getDefault
2+
3+
class Solution {
4+
fun isPalindrome(s: String): Boolean {
5+
if (s.isBlank()) return true
6+
val cleanedString = s.trim().filter { it.isLetterOrDigit() }.lowercase(getDefault())
7+
return cleanedString == cleanedString.reversed()
8+
}
9+
}

0 commit comments

Comments
 (0)