From eddfaee6a8a6a482b4eac81b0dda816fdb645006 Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sat, 9 Aug 2025 10:10:10 +0900 Subject: [PATCH] Week 3 Solutions --- combination-sum/hoyeongkwak.py | 36 +++++++++++++++++++++++++++++++ decode-ways/hoyeongkwak.java | 36 +++++++++++++++++++++++++++++++ maximum-subarray/hoyeongkwak.java | 23 ++++++++++++++++++++ number-of-1-bits/hoyeongkwak.java | 14 ++++++++++++ valid-palindrome/hoyeongkwak.java | 13 +++++++++++ 5 files changed, 122 insertions(+) create mode 100644 combination-sum/hoyeongkwak.py create mode 100644 decode-ways/hoyeongkwak.java create mode 100644 maximum-subarray/hoyeongkwak.java create mode 100644 number-of-1-bits/hoyeongkwak.java create mode 100644 valid-palindrome/hoyeongkwak.java diff --git a/combination-sum/hoyeongkwak.py b/combination-sum/hoyeongkwak.py new file mode 100644 index 0000000000..9d37de8cda --- /dev/null +++ b/combination-sum/hoyeongkwak.py @@ -0,0 +1,36 @@ +''' +Time complexity : O(n ^ (target / min_val)) +Space complexity : O(target / min_val) +backtracking +c = [2], total = 2 +c = [2, 2], total = 4 +c = [2, 2, 2] total = 6 +c = [2, 2, 2, 2] total = 8, 초과 backtrack +c = [2, 2, 2, 3] total = 9, 초과 backtrack +c = [2, 2, 2, 6] total = 12, 초과 backtrack +c = [2, 2, 2, 7] total = 13, 초과 backtrack +c = [2, 2, 3] total = 7 정답 추가 +c = [2, 2, 6] total = 10, 초과 backtrack +c = [2, 2, 7] total = 11, 초과 backtrack +.... +[2,2,3], [7] + +''' + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + result = [] + + def backtrack(start, current, total): + if total == target: + result.append(list(current)) + return + if total > target: + return + + for i in range(start, len(candidates)): + current.append(candidates[i]) + backtrack(i, current, total + candidates[i]) + current.pop() + backtrack(0, [], 0) + return result diff --git a/decode-ways/hoyeongkwak.java b/decode-ways/hoyeongkwak.java new file mode 100644 index 0000000000..4a5ab99716 --- /dev/null +++ b/decode-ways/hoyeongkwak.java @@ -0,0 +1,36 @@ +/* +Time complexity : O(n) +Space complexity : O(n) +dp를 사용 +현재 숫자만 사용(1~9) +이전 숫자와 합쳐서 사용(10~26) +ex) 226 +2 2 6 +22 6 +2 26 +*/ +class Solution { + public int numDecodings(String s) { + if (s == null || s.length() == 0 || s.charAt(0) == '0') { + return 0; + } + + int n = s.length(); + int[] dp = new int[n + 1]; + + dp[0] = 1; + dp[1] = 1; + + for (int i = 2; i <= n; i++) { + int oneDigit = Integer.parseInt(s.substring(i - 1, i)); + if (oneDigit >= 1 && oneDigit <= 9) { + dp[i] += dp[i - 1]; + } + int twoDigits = Integer.parseInt(s.substring(i - 2, i)); + if (twoDigits >= 10 && twoDigits <= 26) { + dp[i] += dp[i - 2]; + } + } + return dp[n]; + } +} diff --git a/maximum-subarray/hoyeongkwak.java b/maximum-subarray/hoyeongkwak.java new file mode 100644 index 0000000000..d9401b46d5 --- /dev/null +++ b/maximum-subarray/hoyeongkwak.java @@ -0,0 +1,23 @@ +/* +Time complexity : O(n) +Space complexity : O(1) +*/ +class Solution { + public int maxSubArray(int[] nums) { + int maxSum = Integer.MIN_VALUE; + int currentSum = 0; + + for (int i = 0; i < nums.length; i++) { + currentSum += nums[i]; + + if (currentSum > maxSum) { + maxSum = currentSum; + } + + if (currentSum < 0) { + currentSum = 0; + } + } + return maxSum; + } +} diff --git a/number-of-1-bits/hoyeongkwak.java b/number-of-1-bits/hoyeongkwak.java new file mode 100644 index 0000000000..0acc6f6b36 --- /dev/null +++ b/number-of-1-bits/hoyeongkwak.java @@ -0,0 +1,14 @@ +/* +Time complexity : O(n) +Space complexity : O(1) +*/ +class Solution { + public int hammingWeight(int n) { + int sum = 0; + while (n != 0) { + n &= (n - 1); + sum++; + } + return sum; + } +} diff --git a/valid-palindrome/hoyeongkwak.java b/valid-palindrome/hoyeongkwak.java new file mode 100644 index 0000000000..89996cbc8c --- /dev/null +++ b/valid-palindrome/hoyeongkwak.java @@ -0,0 +1,13 @@ +/* +Time Complexity : O(n) +Space Complexity : O(n) +*/ +class Solution { + public boolean isPalindrome(String s) { + if (s.length() == 1) return true; + String sLower = s.toLowerCase().replaceAll("[^a-zA-Z0-9]", ""); + StringBuffer strBuilder = new StringBuffer(sLower); + String revStr = strBuilder.reverse().toString(); + return sLower.equals(revStr); + } +}