Skip to content

[hoyeongkwak] Week3 Solutions #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2025
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
36 changes: 36 additions & 0 deletions combination-sum/hoyeongkwak.py
Copy link
Contributor

Choose a reason for hiding this comment

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

백트랙킹 분석과 함께 코드 풀어주신게 설명이 너무 좋네요!

Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions decode-ways/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -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];
}
}
23 changes: 23 additions & 0 deletions maximum-subarray/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 14 additions & 0 deletions number-of-1-bits/hoyeongkwak.java
Copy link
Contributor

Choose a reason for hiding this comment

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

오.. 전 n을 이진수로 바꿔서 맨 오른쪽으로 한칸씩 옮기면서 비트 연산으로 1인지 아닌지를 체크했는데 이런 방법도 있군요.

Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions valid-palindrome/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -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);
}
}