Skip to content
Open
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
32 changes: 30 additions & 2 deletions invert-binary-tree/juhui-jeong.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, DFS, BFS
  • 설명: 이 코드는 이진 트리의 노드를 순회하며 자식 노드들을 교환하는데, BFS는 큐를, DFS는 스택을 활용하여 구현되어 있습니다. 두 방법 모두 트리 구조를 탐색하는 패턴입니다.

📊 시간/공간 복잡도 분석

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

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

피드백: 트리의 모든 노드를 한 번씩 방문하며, 큐를 통해 노드를 저장하는 구조를 사용하였기 때문에 시간 복잡도는 노드 수에 비례하는 O(n)입니다. 큐는 최악의 경우 트리의 높이만큼 저장할 수 있어 공간 복잡도도 O(n)입니다.

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

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

피드백: 모든 노드를 한 번씩 방문하며, 스택에 저장하는 구조를 사용하였기 때문에 시간 복잡도는 노드 수에 비례하는 O(n)입니다. 스택은 트리의 높이만큼 저장할 수 있어 공간 복잡도도 O(n)입니다.

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* 시간 복잡도: O(1)
* 공간 복잡도: O(1)
이전 풀이
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
Expand Down Expand Up @@ -30,3 +31,30 @@ public TreeNode invertTree(TreeNode root) {
return root;
}
}

/**
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
* DFS 풀이
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) return null;

Deque<TreeNode> stack = new ArrayDeque<>();
stack.push(root);

while(!stack.isEmpty()) {
TreeNode cur = stack.pop();

TreeNode temp = cur.left;
cur.left = cur.right;
cur.right = temp;

if (cur.left != null) stack.push(cur.left);

if (cur.right != null) stack.push(cur.right);
}
return root;
}
}
34 changes: 34 additions & 0 deletions search-in-rotated-sorted-array/juhui-jeong.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
  • 설명: 이 코드는 정렬된 배열에서 특정 값을 찾기 위해 이진 탐색을 활용하며, 회전된 배열에서도 효율적으로 검색하는 패턴입니다.

📊 시간/공간 복잡도 분석

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

피드백: 이진 탐색을 활용하여 배열의 회전 여부에 따라 탐색 범위를 조절하는 방식으로, 배열 전체를 한 번씩 순회하지 않고 로그 시간에 해결합니다. 공간은 변수 몇 개만 사용하므로 상수입니다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 시간 복잡도: O(log n)
* 공간 복잡도: O(1)
*/
class Solution {
public int search(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;

while (low <= high) {
int mid = (low + high) / 2;

if (nums[mid] == target) {
return mid;
}
// 왼쪽 구간이 정렬된 경우
if (nums[low] <= nums[mid]) {
if (nums[low] <= target && target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
// 오른쪽 구간이 정렬된 경우
} else {
if (nums[mid] < target && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
return -1;
}
}
Loading