Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
20 changes: 20 additions & 0 deletions climbing-stairs/sonshn.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
  • 설명: 이 코드는 각 단계의 경우의 수를 이전 두 값의 합으로 계산하는 피보나치형 점화식 DP로 풀이합니다. 배열을 이용한 상태 저장과 반복을 통한 계산으로 문제를 해결합니다.

📊 시간/공간 복잡도 분석

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

피드백: 초기 예외 처리와 DP 배열 사용으로 각 n에 대해 한 번씩 계산한다. 연산은 선형적이다.

개선 제안: 현재 구현이 적절해 보입니다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* dp[i] = dp[i - 1] + dp[i - 2]
*/
class Solution {
public int climbStairs(int n) {
if (n <= 2) {
return n;
}

int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;

for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}

return dp[n];
}
}
57 changes: 57 additions & 0 deletions maximum-depth-of-binary-tree/sonshn.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Depth-First Search, Breadth-First Search, Binary Search
  • 설명: 코드는 재귀적으로 트리의 깊이를 구하는 DFS와 반복적으로 레벨 단위로 트리를 순회하는 BFS를 모두 포함합니다. 두 방식 모두 트리의 높이나 노드 수를 이용한 탐색 패턴에 해당합니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.maxDepth — Time: ✅ O(n) → O(n) / Space: ❌ O(h) → O(n)
유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(h) O(n)

피드백: 트리 각 노드를 한 번씩 방문하므로 시간 복잡도는 노드 수에 선형적입니다. 재귀 호출 깊이는 트리의 최대 높이에 의해 공간이 결정됩니다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: Solution.maxDepth — Time: ✅ O(n) → O(n) / Space: ✅ O(n) → O(n)
유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(n) O(n)

피드백: 모든 노드를 한 번씩 방문하고 큐를 사용하여 레벨을 구분합니다. 최악의 경우 큐에 많은 노드가 남아 있을 수 있습니다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 이진 트리의 최대 깊이를 재귀적으로 계산 (DFS)
*
* 시간 복잡도: O(n), n은 트리의 노드 수
* 공간 복잡도: O(h), h는 트리의 높이, 재귀 호출로 인해 스택에 쌓이는 함수 호출의 깊이 때문
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}

int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);

return Math.max(leftDepth, rightDepth) + 1;
}
}

/**
* 이진 트리의 최대 깊이를 반복적으로 계산 (BFS)
*
* 시간 복잡도: O(n), n은 트리의 노드 수
* 공간 복잡도: O(n), 큐에 저장되는 노드 수 때문
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);

int depth = 0;

while (!queue.isEmpty()) {
int size = queue.size();

for (int i = 0; i < size; i++) {
TreeNode current = queue.poll();

if (current.left != null) {
queue.offer(current.left);
}

if (current.right != null) {
queue.offer(current.right);
}
}

depth++;
}

return depth;
}
}
20 changes: 20 additions & 0 deletions merge-two-sorted-lists/sonshn.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.

재귀로 푸셨군요. 엄청 간결하고 직관적이어서 좋은 거 같습니다.

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Divide and Conquer, Two Pointers
  • 설명: 두 정렬 리스트를 재귀적으로 병합하는 방식은 문제를 반으로 나눠 해결하는 듯한 Divide and Conquer 성격과, 각 단계에서 작은 문제의 해를 연결해 전체를 구성하는 특징이 있습니다. 또한 두 리스트를 한 방향으로 비교하며 포인터를 이동시키는 양방향 탐색의 연속 처리로 Two Pointers 패턴도 적용됩니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(n + m) O(n + m)
Space O(n + m) O(n + m)

피드백: 재귀 호출 스택으로 인해 추가 공간이 필요하지만 각 재귀 호출마다 상위 노드 하나씩 차지합니다. 합병 자체는 선형 시간에 수행됩니다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 재귀를 사용하여 두 개의 정렬된 연결 리스트를 병합
*
* 시간 복잡도: O(n + m), n은 list1의 길이, m은 list2의 길이
* 공간 복잡도: O(n + m), 재귀 호출로 인해 스택에 쌓이는 함수 호출의 깊이 때문
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;

if (list1.val <= list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
}
19 changes: 19 additions & 0 deletions number-of-1-bits/sonshn.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의 개수를 세는 연산으로, 비트 단위 연산과 시프트를 이용한 패턴이다. n & 1으로 최하위 비트를 확인하고 오른쪽으로 이동시키는 방식이 핵심이다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(1) O(k)
Space O(1) O(1)

피드백: 비트 시프트를 통해 1의 개수를 누적한다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* hammingWeight
* 정수 n의 이진수 표현에서 1의 개수를 반환
*
* 시간 복잡도: O(1)
* 공간 복잡도: O(1)
*/
public class sonshn {
public int hammingWeight(int n) {
int count = 0;

while (n != 0) {
count += (n & 1);
n >>>= 1;
}

return count;
}
}
19 changes: 19 additions & 0 deletions valid-anagram/sonshn.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search, Monotonic Stack, Hash Map / Hash Set, Greedy, Divide and Conquer, Dynamic Programming, Two Pointers, Sliding Window, Fast & Slow Pointers, BFS, DFS, Backtracking, Union Find, Trie, Bit Manipulation
  • 설명: 주어진 코드는 문자열을 문자 배열로 변환 후 정렬해서 비교하는 방식으로, 요소 간의 순서를 무시한 동등성 판단을 다룬다. 정렬 자체가 핵심 연산이며, 이를 통해 두 문자열의 구성 문자가 동일한지 확인하는 패턴이다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(nlogn) O(n log n + m log m)
Space O(n) O(n + m)

피드백: 정렬 기반으로 동등 여부를 판단한다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.*;

/**
* String to char array, sort, and compare
*
* 시간 복잡도: O(nlogn)
* 공간 복잡도: O(n)
*/
class Solution {
public boolean isAnagram(String s, String t) {
char[] sArray = s.toCharArray();
char[] tArray = t.toCharArray();

Arrays.sort(sArray);
Arrays.sort(tArray);

return Arrays.equals(sArray, tArray);
}
}
36 changes: 36 additions & 0 deletions valid-palindrome/sonshn.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, Greedy
  • 설명: 두 포인터(left, right)를 양 끝에서 시작해 문자 비교 전 특수문자 제거 및 소문자 비교를 진행하는 패턴으로, 한 번의 순회로 조건 검사와 종료를 수행합니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(1) O(1)

피드백: 공백/기호 제거 및 소문자 비교를 수행한다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Palindrome 문자열인지 확인하는 문제
*
* Java에서 제공하는 Character.isLetterOrDigit() 메서드를 사용하여 알파벳과 숫자인지 확인하고,
* Character.toLowerCase() 메서드를 사용하여 대소문자를 구분하지 않고 비교
*
* 시간 복잡도: O(n)
* 공간 복잡도: O(1)
*/
class Solution {
public boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;

while (left < right) {

while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
left++;
}

while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
right--;
}

if (Character.toLowerCase(s.charAt(left))
!= Character.toLowerCase(s.charAt(right))) {
return false;
}

left++;
right--;
}

return true;
}
}
Loading