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
48 changes: 48 additions & 0 deletions kth-smallest-element-in-a-bst/hyeri0903.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
  • 설명: 이 코드는 이진 탐색 트리에서 중위 순회(DFS)를 통해 k번째 작은 값을 찾는 방식으로 동작하며, Binary Search와 DFS 패턴이 적용됩니다.

📊 시간/공간 복잡도 분석

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

피드백: 이 구현은 재귀적 중위순회로, 최악의 경우 트리의 높이 h만큼의 스택 공간을 사용하며, 모든 노드를 방문하므로 시간 복잡도는 O(n)이다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int count = 0;
private int answer = 0;
public int kthSmallest(TreeNode root, int k) {
/**
1.BST에서 K번째 가장 작은 값 찾기
2.constraints : node 개수 ㅡin = 0, max = 10000
3.solutions:
- k번째 방문한 노드가 k번째로 작은 값 (bst 이므로)
- dfs search (inorder traversal)
*/

dfs(root, k);
return answer;
}
private void dfs(TreeNode node, int k) {
if(node == null) {
return;
}


dfs(node.left, k);
count++;

if(count == k) {
answer = node.val;
return;
}

dfs(node.right, k);
}

}
30 changes: 30 additions & 0 deletions non-overlapping-intervals/hyeri0903.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
  • 설명: 이 코드는 겹치지 않는 구간을 찾기 위해 끝나는 시간 기준으로 정렬 후 선택하는 그리디 전략을 사용합니다. 최소 제거 개수를 구하는 문제에 적합한 패턴입니다.

📊 시간/공간 복잡도 분석

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

피드백: intervals 배열을 정렬하는데 O(n log n) 시간이 소요되고, 이후 한 번 순회하며 겹침 여부를 체크하므로 전체 시간 복잡도는 O(n log n)이다. 공간은 정렬에 필요한 상수 공간만 사용한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
/**
1.prob: 겹치지 않는 최소한의 non-oerlapping interval 제거
2.constraints
- inverval.lenght min=1, max = 100,000
3.solutions - Greedy
- end 기준 오름차순 정렬
- 이전 interval end 저장
- 다음 interval check, 안겹치면 -> 선택, 겹치면 count++
Time: O(n logn), Space: O(1)
*/
int count = 0;

//end 기준 ascending 정렬
Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
int end = intervals[0][1];

//겹치면 count++, 안겹치면 end update
for(int i = 1; i < intervals.length; i++) {
if(intervals[i][0] < end) {
count++;
} else {
end = intervals[i][1];
}
}

return count;
}
}
46 changes: 46 additions & 0 deletions remove-nth-node-from-end-of-list/hyeri0903.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
  • 설명: 이 코드는 fast와 slow 두 포인터를 이용하여 리스트의 뒤에서 n번째 노드를 찾고 제거하는 방식으로, 두 포인터 패턴에 속합니다.

📊 시간/공간 복잡도 분석

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

피드백: 이중 포인터를 사용하여 한 번의 순회로 해결하며, 리스트 길이와 상관없이 일정한 공간을 사용한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
/**
1.linked list 에서 뒤에서 n번째 node remove
2.constraints:
node 개수(sz) min=1, max=30
n값 min = 1, max= sz
3.solutions: two pointers
- fast, slow pointer 2개의 간격 = n
- fast 가 끝지점에 도달하면 그때 slow.next 를 제거
time: O(n), space: O(1)
*/

ListNode dummy = new ListNode(0);
dummy.next = head;

ListNode fast = dummy;
ListNode slow = dummy;

//fast pointer n+1칸 이동, fast - slow = n
for(int i = 0; i <= n; i++) {
fast = fast.next;
}

while(fast != null) {
fast = fast.next;
slow = slow.next;
}

//slow pointer 의 다음 노드(slow.next) 제거
slow.next = slow.next.next;

return dummy.next;

}
}
44 changes: 44 additions & 0 deletions same-tree/hyeri0903.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) 방식으로 두 트리의 구조와 값을 비교하여 동일한지 판단합니다.

📊 시간/공간 복잡도 분석

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

피드백: 두 트리의 모든 노드를 방문하며, 최악의 경우 트리의 높이 h만큼의 재귀 호출 스택을 사용한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
/**
1.problem: 2개의 bst 가 동일한지 체크
2.constraints - 구조와 노드가 동일해야된다
3.solution - DFS
1) 두 트리의 노드가 모두 null -> true
2) 둘중 하나만 null -> false
3) 값이 다르면 false
4)재귀로 트리의 구조가 동일한지 체크
*/

if(p == null && q == null) {
return true;
}
if(p == null || q == null) {
return false;
}
if(p.val != q.val) {
return false;
}

if(isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) {
return true;
}
return false;
}

}
Loading