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
56 changes: 56 additions & 0 deletions course-schedule/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(V + E)
Space O(V + E)

피드백: 그래프를 인접 리스트로 표현하고, DFS를 통해 사이클을 탐지하는 방식으로 정점과 간선 수에 비례하는 시간과 공간 복잡도를 갖는다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
/**
1.문제: [a, b] = b를 수강해야 a 수강 가능, 모두 수강가능하면 true, 아니면 flase return
2.constraints
- numCourses min = 1, max = 2000
- prerequisites length min = 0, max = 5000
3.solution
- dfs, graph에 cycle 이 존재하는지 check. cycle 이 존재하면 수강 다 못하므로 false return
*/
List<List<Integer>> graph = new ArrayList<>();
for(int i = 0; i < numCourses; i++) {
graph.add(new ArrayList<>());
}

//graph 생성
for(int i = 0; i < prerequisites.length; i++) {
int a = prerequisites[i][0];
int b = prerequisites[i][1];

graph.get(b).add(a);
}

//dfs cycle check
//state: 0 = 방문 전, 1 = 방문 중, 2 = 방문 완료
int[] state = new int[numCourses];
//node 개수(numCourses)만큼 순회
for(int i = 0; i < numCourses; i++) {
//cycle 존재하면 false return
if(dfs(i, graph, state)) {
return false;
}
}
return true;
}


boolean dfs(int i, List<List<Integer>> graph, int[] state) {
//순회 cycle 발견하면 true
if(state[i] == 1) {
return true;
}
if(state[i] == 2) {
return false;
}
state[i] = 1;

for(int next: graph.get(i)) {
if(dfs(next, graph, state)) {
return true;
}
}
state[i] = 2;
return false;
}
}
52 changes: 52 additions & 0 deletions invert-binary-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)

피드백: 각 노드를 한 번씩 방문하며, 재귀 호출로 인해 최대 트리 높이만큼의 스택 공간이 필요하다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 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 TreeNode invertTree(TreeNode root) {
/**
1.문제: inverted binary tree 출력
2.constraints: node 개수 min = 0, max = 100
3.solution: left, right node swap
time complexity:
- BST인 경우 best case : O(log n)
- skwed 인 경우 worst case: O(n)
- space complexity: O(h)
*/

if(root == null) {
return null;
}

dfs(root);

return root;

}

void dfs(TreeNode root) {
if(root == null) {
return;
}
//swap
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;

//left recursion
dfs(root.left);
//right recursion
dfs(root.right);
}
}
26 changes: 26 additions & 0 deletions jump-game/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)
Space O(1)

피드백: 한 번의 반복으로 최대 도달 인덱스를 갱신하며, 배열 길이만큼의 시간과 상수 공간을 사용한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public boolean canJump(int[] nums) {
/**
1.문제: last index 에 도달 가능하면 true, 아니면 false return
2.constraints
- first index 에서 시작
- nums.length min = 1, max = 10000
- value min = 0, max = 100000
3.solution
- brute force: 0 index 부터 1, 2, 3번째 등등 시도, time: O(n^2)
- 가장 먼 index 을 업데이트하면서 체크 : time O(n)
*/
int n = nums.length;
int farthest = 0;

for(int i = 0; i < n; i++) {
//현재까지 도달 가능한지 체크
if(i > farthest) {
return false;
}
//도달가능한 가장 먼 인덱스 update
farthest = Math.max(farthest, i + nums[i]);
}
return true;
}
}
52 changes: 52 additions & 0 deletions merge-k-sorted-lists/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Heap / Priority Queue
  • 설명: 이 코드는 여러 정렬된 리스트를 병합하기 위해 최소 힙을 사용하여 노드를 선택하고 연결하는 방식으로 해결한다. 힙 자료구조를 활용한 우선순위 큐 패턴이 핵심이다.

📊 시간/공간 복잡도 분석

복잡도
Time O(N log k)
Space O(k)

피드백: 모든 노드를 힙에 넣고 하나씩 꺼내며 병합하는 방식으로, 노드 수 N과 리스트 수 k에 비례하는 시간과 공간 복잡도를 갖는다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 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 mergeKLists(ListNode[] lists) {
/**
1.문제: ascending liknked list 를 모두 sorted list 로 머지해라.
2.조건
- k = list 길이, 최소 = 0, 최대 = 10^4
- 원소값 0 이상, 500 이하
3.풀이
- priority queue(min-heap): time = O(n log k), space = O(k)
- 각 list 를 min heap 에 넣고 하나씩 뽑아서 새 리스트에 연결.
- Heap 에 들어가고 나올때마다 O(log k), 총 노드 수 N = O(N logk)
- Heap 사용 -> space = k
*/

if(lists == null || lists.length == 0) return null;

PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val);

//all list의 첫 노드 넣기
for(ListNode node: lists) {
if (node != null) {
pq.offer(node);
}
}

//결과 리스트를 위한 더미 노드
ListNode dummy = new ListNode(0);
ListNode curr = dummy;

while(!pq.isEmpty()) {
ListNode node = pq.poll(); //최소값 pop
curr.next = node; //결과 리스트에 연결
curr = curr.next; //포인터 이동
//다음 노드를 heap에 insert
if(node.next != null) {
pq.offer(node.next);
}
}
return dummy.next;
}
}
44 changes: 44 additions & 0 deletions search-in-rotated-sorted-array/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
  • 설명: 이 코드는 회전된 정렬 배열에서 목표 값을 찾기 위해 이진 탐색 방식을 사용합니다. 배열의 특성을 고려하여 범위를 좁혀가며 검색하는 방식이 핵심입니다.

📊 시간/공간 복잡도 분석

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

피드백: 배열의 회전 여부를 판단하여, 조건에 따라 좌우 범위를 좁혀가며 이진 탐색을 수행한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
public int search(int[] nums, int target) {
/**
1.prob: index k기준으로 left rotated 된 array 에서 target index return, 존재하지 않으면 -1 return
2.constraints
- asc 정렬된 배열, 모두 unique 한 값
- 반드시 O(log n) 으로 풀 것
- num.length min=1, max = 5000
3.solution
- bruteforce, for문 -> time: O(n)
- binary search -> time: O(log n), space: O(1)
*/

int n = nums.length;
int left = 0, right = n - 1;


while(left <= right) {
int mid = (left + right) / 2;

//target 찾으면 index return
if(nums[mid] == target) {
return mid;
}

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