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
38 changes: 38 additions & 0 deletions insert-interval/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 정렬된 구간 배열을 순회하며 두 포인터를 활용해 새 구간을 적절히 삽입하고 병합하는 방식으로 문제를 해결합니다. 따라서 Two Pointers 패턴에 속합니다.

📊 시간/공간 복잡도 분석

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

피드백: 단일 순회로 구간들을 검사하며 새 구간과 겹치는 구간을 병합하거나 위치에 맞게 삽입하므로 시간 복잡도는 O(n), 추가 공간은 결과 리스트 저장을 위해 O(n)입니다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
*/
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> result = new ArrayList<>();

int newStart = newInterval[0];
int newEnd = newInterval[1];
boolean inserted = false;

for (int i = 0; i < intervals.length; i++) {
int curStart = intervals[i][0];
int curEnd = intervals[i][1];

if (curEnd < newStart) {
// 1. newInterval 보다 왼쪽
result.add(new int[]{curStart, curEnd});
} else if (curStart > newEnd) {
// 2. newInterval 보다 오른쪽
if (!inserted) {
result.add(new int[]{newStart, newEnd});
inserted = true;
}
result.add(new int[]{curStart, curEnd});
} else {
newStart = Math.min(newStart, curStart);
newEnd = Math.max(newEnd, curEnd);
}
}

if (!inserted) {
result.add(new int[]{newStart, newEnd});
}
return result.toArray(new int[result.size()][]);
}
}
19 changes: 19 additions & 0 deletions kth-smallest-element-in-a-bst/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, Inorder Traversal
  • 설명: 이 코드는 이진 탐색 트리의 중위 순회를 통해 정렬된 값을 얻고, k번째 작은 값을 찾는 방식으로 동작합니다. 중위 순회는 이진 탐색 트리의 특성을 활용하는 대표적인 패턴입니다.

📊 시간/공간 복잡도 분석

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

피드백: 중위 순회로 모든 노드를 방문하여 정렬된 리스트를 만들기 때문에 시간과 공간 복잡도는 각각 O(n)입니다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
*/
class Solution {
public int kthSmallest(TreeNode root, int k) {
List<Integer> list = new ArrayList<>();
inorder(root, list);

return list.get(k-1);
}
private void inorder(TreeNode node, List<Integer>list) {
if (node == null) return;

inorder(node.left, list);
list.add(node.val);
inorder(node.right, list);
}
}
19 changes: 18 additions & 1 deletion lowest-common-ancestor-of-a-binary-search-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
  • 설명: 이 코드는 이진 탐색 트리의 특성을 이용하여 p와 q의 값을 비교하며 루트를 탐색하는 방식으로 최적의 공통 조상을 찾는다. 값 비교를 통해 탐색 범위를 좁혀가는 이진 탐색 패턴이 적용되어 있다.

📊 시간/공간 복잡도 분석

복잡도
Time O(h)
Space O(1)

피드백: BST의 특성을 이용하여 루트에서부터 탐색하며, 최악의 경우 트리 높이 h만큼의 재귀 또는 반복이 필요하므로 시간 복잡도는 O(h), 공간은 재귀 호출 스택 또는 반복 변수에 따라 O(h) 또는 O(1)입니다.

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

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

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
이전 풀이(반복문)
시간복잡도: O(log N)
공간복잡도: O(1)
*/

class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
while(root != null) {
Expand All @@ -16,3 +17,19 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
return null;
}
}
*/

/**
시간복잡도: O(h)
공간복잡도: O(h)
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (p.val < root.val && q.val < root.val) {
return this.lowestCommonAncestor(root.left, p, q);
} else if (p.val > root.val && q.val > root.val) {
return this.lowestCommonAncestor(root.right, p, q);
}
return root;
}
}
68 changes: 48 additions & 20 deletions non-overlapping-intervals/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Greedy, Sorting
  • 설명: 이 코드는 정렬 후 겹침 여부를 판단하는 그리디 전략을 사용하며, 최적의 선택을 위해 정렬이 핵심입니다. 정렬을 통해 효율적으로 겹침을 체크하는 방식입니다.

📊 시간/공간 복잡도 분석

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

피드백: 구간 정렬 후 한 번의 순회로 겹치는 구간을 계산하므로 시간 복잡도는 정렬에 따른 O(n log n), 공간은 상수입니다.

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

Original file line number Diff line number Diff line change
@@ -1,51 +1,79 @@
/*
* 끝나는 시간 오름차순 정렬
*/
처음 풀이
시작 시간 기준 정렬
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (a,b) -> Integer.compare(a[1], b[1]));
Arrays.sort(intervals, (a,b) -> {
if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
return Integer.compare(a[1], b[1]);
});

int removeCntResult = 0;
int end = intervals[0][1];
int[] cur = intervals[0];

for (int i = 1; i< intervals.length; i++) {
if (intervals[i][0] < end) {
int [] next = intervals[i];

if (cur[1] > next[0]) {
removeCntResult += 1;
if (next[1] < cur[1]) {
cur = next;
}
} else {
end = intervals[i][1];
cur = next;
}
}
return removeCntResult;
}
}
*/


/*
처음 풀이
시작 시간 기준 정렬
* 끝나는 시간 오름차순 정렬

class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (a,b) -> {
if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
return Integer.compare(a[1], b[1]);
});
Arrays.sort(intervals, (a,b) -> Integer.compare(a[1], b[1]));

int removeCntResult = 0;
int[] cur = intervals[0];
int end = intervals[0][1];

for (int i = 1; i< intervals.length; i++) {
int [] next = intervals[i];

if (cur[1] > next[0]) {
if (intervals[i][0] < end) {
removeCntResult += 1;
if (next[1] < cur[1]) {
cur = next;
}
} else {
cur = next;
end = intervals[i][1];
}
}
return removeCntResult;
}
}
*/

/**
* 시간 복잡도: O(n log n)
* 공간 복잡도: O(n)
*/
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (a,b) -> a[0] - b[0]);
int count = 0;
int prevEnd = intervals[0][1];

for (int i = 1; i < intervals.length; i++) {
int curStart = intervals[i][0];
int curEnd = intervals[i][1];

if (prevEnd <= curStart) {
// 안겹치는 경우(변경 없이 다음 배열로)
prevEnd = curEnd;
} else {
// 겹치는 경우
count++;
prevEnd = Math.min(prevEnd, curEnd);
}
}
return count;
}
}
61 changes: 46 additions & 15 deletions remove-nth-node-from-end-of-list/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 두 포인터(빠른, 느린)를 활용하여 리스트의 끝에서 n번째 노드를 찾고 제거하는 방식으로 효율적입니다.

📊 시간/공간 복잡도 분석

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

피드백: 빠른 포인터를 n만큼 이동시키고, 느린 포인터와 함께 한 번 순회하여 제거 위치를 찾기 때문에 시간 복잡도는 O(n), 공간 복잡도는 O(1)입니다.

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

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

Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@

/*
첫 번째 풀이
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
List<ListNode> list = new ArrayList<>();

while(head != null) {
list.add(head);
head = head.next;
}

list.remove(list.size()-n);
if (list.isEmpty()) return null;

for (int i = 0; i < list.size()-1; i++) {
list.get(i).next = list.get(i+1);
}
list.get(list.size()-1).next = null;
return list.get(0);
}
}
*/


/*
시간 복잡도: O(N)
공간 복잡도: O(1)
*/

class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
Expand All @@ -24,26 +49,32 @@ public ListNode removeNthFromEnd(ListNode head, int n) {
return dummy.next;
}
}
*/

/*
첫 번째 풀이
/**
*
* 시간 복잡도는 O(n)
* 공간 복잡도는 O(1)
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
List<ListNode> list = new ArrayList<>();

while(head != null) {
list.add(head);
head = head.next;
int length = 0;
ListNode curLenCheck = head;

while(curLenCheck != null) {
length++;
curLenCheck = curLenCheck.next;
}

list.remove(list.size()-n);
if (list.isEmpty()) return null;
int targetIndex = length - n;
if (targetIndex == 0) return head.next;

for (int i = 0; i < list.size()-1; i++) {
list.get(i).next = list.get(i+1);
ListNode targetPrev = head;
for (int i=0; i < targetIndex-1; i++) {
targetPrev = targetPrev.next;
}
list.get(list.size()-1).next = null;
return list.get(0);
targetPrev.next = targetPrev.next.next;

return head;
}
}
*/
30 changes: 30 additions & 0 deletions same-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.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS, BFS
  • 설명: 이 코드는 재귀를 이용한 DFS와 반복문을 이용한 BFS 두 가지 방법으로 트리의 노드를 순회하며 비교하는 방식입니다. 두 패턴 모두 트리 구조 탐색에 자주 사용됩니다.

📊 시간/공간 복잡도 분석

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

피드백: 모든 노드를 방문하는 재귀 또는 반복으로 구조와 값을 비교하므로 시간 복잡도는 O(n), 공간은 재귀 호출 또는 큐의 크기에 따라 O(h)입니다.

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

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,33 @@ public boolean isSameTree(TreeNode p, TreeNode q) {
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}

/*
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
Queue<TreeNode> queueP = new LinkedList<>();
Queue<TreeNode> queueQ = new LinkedList<>();

queueP.offer(p);
queueQ.offer(q);

while(!queueP.isEmpty() && !queueQ.isEmpty()) {
TreeNode curP = queueP.poll();
TreeNode curQ = queueQ.poll();

if (curP == null && curQ == null) continue;
if (curP == null || curQ == null) return false;

if (curP.val != curQ.val) return false;

queueP.offer(curP.left);
queueQ.offer(curQ.left);
queueP.offer(curP.right);
queueQ.offer(curQ.right);
}
return true;
}
}
43 changes: 43 additions & 0 deletions serialize-and-deserialize-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.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 트리의 직렬화와 역직렬화를 위해 깊이 우선 탐색(DFS) 방식을 사용하여 재귀적으로 노드를 처리합니다.

📊 시간/공간 복잡도 분석

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

풀이 1: Codec.serialize — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: 모든 노드를 방문하여 문자열로 직렬화하므로 시간과 공간 복잡도는 각각 O(n)입니다.

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

풀이 2: Codec.deserialize — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: 입력 문자열을 순차적으로 읽으며 재귀 호출로 트리 복원, 시간과 공간 모두 O(n)입니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

public class Codec {
private int index;

// Encodes a tree to a single string.(직렬화)
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
dfsSerialize(root, sb);
return sb.toString();
}

private void dfsSerialize(TreeNode node, StringBuilder sb) {
if (node == null) {
sb.append("null").append(",");
return;
}
sb.append(node.val).append(",");

dfsSerialize(node.left, sb);
dfsSerialize(node.right, sb);
}

// Decodes your encoded data to tree.(역직렬화)
public TreeNode deserialize(String data) {
String[] arr = data.split(",");
index = 0;
return dfsDeserialize(arr);
}

private TreeNode dfsDeserialize(String[] arr) {
String value = arr[index++];

if (value.equals("null")) {
return null;
}

TreeNode node = new TreeNode(Integer.parseInt(value));

node.left = dfsDeserialize(arr);
node.right = dfsDeserialize(arr);
return node;
}
}
Loading