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
35 changes: 35 additions & 0 deletions combination-sum/hoonjichoi1.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Backtracking, Depth-First Search, Greedy
  • 설명: dfs 재귀로 모든 가능한 조합을 탐색하며 합이 타깃과 같아지면 결과에 추가하는 방식으로 문제를 해결한다. 각 재귀에서 선택/포기 결정이 많아 백트래킹의 전형적 구조를 띄고 있고, 깊이 우선 탐색의 탐색 흐름을 따른다.

📊 시간/공간 복잡도 분석

복잡도
Time O(k * t)
Space O(t)

피드백: 백트래킹으로 모든 가능한 조합을 탐색하고 중복 없이 시작 위치를 고정하여 재귀를 진행합니다.

개선 제안: 현재 구현은 시간 복잡도가 후보 수와 목표합에 따라 크게 달라질 수 있습니다. 정렬 후 가지치기나 중복 제거를 추가하면 성능이 개선될 수 있습니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Time Complexity : O(c^t)
Space Complexity : O(t)
*/

class Solution {

public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> output = new ArrayList<>();
Stack<Integer> nums = new Stack<>();
dfs (candidates, output, target, nums, 0, 0);
return output;
}

private void dfs(int[] candidates, List<List<Integer>> output, int target, Stack<Integer> nums, int start, int total) {
// base case : pass
if (target == total) {
output.add(new ArrayList<>(nums));
return;
}
// base case : fail
if (target < total) {
return;
}

for (int i = start ; i < candidates.length ; i++) {
int num = candidates[i];
nums.push(num);
dfs(candidates, output, target, nums, i, total + num);
nums.pop();
}


}
}
25 changes: 25 additions & 0 deletions decode-ways/hoonjichoi1.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming, Divide and Conquer
  • 설명: 주어진 코드는 앞에서의 결정(dp[i] 누적)으로 전체 해를 합산하는 전형적인 DP 패턴이며, 각 위치의 해를 두 가지 경우(dp[i+1], dp[i+2])로 합산합니다. 부분문제의 중복 해결이 핵심입니다. 또한 문제의 재귀적 분할 형태가 바탕이므로 Divide and Conquer 성격도 보일 수 있습니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 두 자리 수 해석 여부를 확인하며 순차적으로 DP 배열을 채웁니다. 0 처리와 경계 조건에 주의가 필요합니다.

개선 제안: 상수 공간으로 최적화 가능(두 변수만 사용하는 최적화). 예외 처리도 간단히 추가하면 안정적입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
dp[i] = dp[i-1] + dp[i-2]
Time Complexity : O(n)
Space Complexity : O(n)
*/
class Solution {
public int numDecodings(String s) {
int n = s.length();
int[] dp = new int[n + 1];
dp[n] = 1;

for (int i = n-1; i > -1; i--) {
if (s.charAt(i) != '0') {
dp[i] += dp[i+1];
}
if (s.charAt(i) != '0' && i+1 < n) {
int twoDigit = Integer.valueOf(s.substring(i, i+2));
if (twoDigit > 0 && twoDigit < 27) {
dp[i] += dp[i+2];
}
}
}
Comment on lines +12 to +22

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

twoDigit >= 10 인 케이스만 존재해서 > 0 보다는 >= 10 으로 좁혀주는게 좋을 거 같습니다!

Suggested change
for (int i = n-1; i > -1; i--) {
if (s.charAt(i) != '0') {
dp[i] += dp[i+1];
}
if (s.charAt(i) != '0' && i+1 < n) {
int twoDigit = Integer.valueOf(s.substring(i, i+2));
if (twoDigit > 0 && twoDigit < 27) {
dp[i] += dp[i+2];
}
}
}
for (int i = n-1; i > -1; i--) {
if (s.charAt(i) != '0') {
dp[i] += dp[i+1];
}
if (s.charAt(i) != '0' && i+1 < n) {
int twoDigit = Integer.valueOf(s.substring(i, i+2));
if (twoDigit >= 10 && twoDigit < 27) {
dp[i] += dp[i+2];
}
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

아 그렇네요! 피드백 감사합니다!

return dp[0];
}
}
19 changes: 19 additions & 0 deletions number-of-1-bits/hoonjichoi1.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Bit Manipulation
  • 설명: 주어진 코드는 정수를 이진수로 보고 1의 개수를 세는 방식으로 비트 수를 계산합니다. 비트 연산 대신 나눗셈 모듈로를 활용해 각 자리의 1을 셈으로써 비트 패턴을 추적합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(log n)
Space O(1)

피드백: 입력 n의 이진 표현 길이만큼 반복하며 각 비트를 확인합니다.

개선 제안: n이 0일 때의 예외 처리와 루프 조건을 명확히 하면 더 안전합니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int hammingWeight(int n) {

if (n == 1) {
return 1;
}

int curr = n, result = 1;
while (curr > 1) {
if (curr % 2 == 1) {
result++;
}
curr = curr / 2;
}

return result;
}
Comment on lines +2 to +17

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

루프 조건을 조금 바꿔주면 n == 1 처리를 별도로 해주지 않아도 될 거 같습니다!

Suggested change
public int hammingWeight(int n) {
if (n == 1) {
return 1;
}
int curr = n, result = 1;
while (curr > 1) {
if (curr % 2 == 1) {
result++;
}
curr = curr / 2;
}
return result;
}
public int hammingWeight(int n) {
int curr = n, result = 0;
while (curr > 0) {
if (curr % 2 == 1) {
result++;
}
curr = curr / 2;
}
return result;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

일부러 1이면 루프를 돌지 않게 base case를 추가한건데 없는게 더 효율적인 코드일까요?!

}

23 changes: 23 additions & 0 deletions valid-palindrome/hoonjichoi1.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Hash Map / Hash Set
  • 설명: 문자열을 정제한 뒤 좌우 포인터를 동시에 이동시키며 대칭 여부를 확인하는 두 포인터 방식이 핵심 패턴입니다. 추가적으로 문자열 비교를 위해 문자 배열화하는 과정은 두 포인터의 필요 조건을 마련합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 비알파벳/숫자 문자 제거와 정규화를 거쳐 양끝 비교를 수행합니다.

개선 제안: 대체로 정규식을 사용한 전처리 대신 두 포인터로 직접 검사하는 방식도 고려해볼 만합니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public boolean isPalindrome(String s) {

// removing all non-alphanumeric characters and convert them to lower case
String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

오타가 있네요

Suggested change
String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
String converted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

Comment on lines +4 to +5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

실제로 돌려보니 replaceAll 비용이 비싸서 runtime 성능이 좋지는 않게 나오는 거 같아요.
직접 순회하는 방식이나 투포인터 등 다른 방식으로도 풀어보시면 좋을 거 같습니다.

구현체도 참고차 남겨드려요!
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/regex/Matcher.java#L1221-L1234


// early return of the empty string case
if (conveted.length() == 0) {
return true;
}

// check the symmetry
int left = 0, right = conveted.length() - 1;
while (left <= right) {
if (conveted.charAt(left) != conveted.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
Loading