-
-
Notifications
You must be signed in to change notification settings - Fork 361
[JeonJe] WEEK 04 Solutions #2742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(log n) | ||
| // SC: O(1) | ||
| class Solution { | ||
| public int findMin(int[] nums) { | ||
|
|
||
| int lowIndex = 0; | ||
| int highIndex = nums.length - 1; | ||
|
|
||
| while(lowIndex < highIndex) { | ||
| int midIndex = (lowIndex + highIndex) / 2; | ||
|
|
||
| //오른 부분이 정렬 되지 않음 = 왼쪽 부분은 정렬됨 | ||
| if(nums[midIndex] > nums[highIndex]) { | ||
| lowIndex = midIndex + 1; | ||
| } else { | ||
| //오른쪽 부분이 정렬 됨 = 왼쪽 부분을 봐야함 | ||
| highIndex = midIndex; | ||
| } | ||
| } | ||
| return nums[lowIndex]; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 깊이 우선 탐색으로 모든 노드를 한 번씩 방문하여 깊이를 누적합니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(h) | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| return calMaxDepth(root); | ||
| } | ||
|
|
||
| // 현재까지 최대 깊이는 Math.max(왼쪽 서브노드 최대 깊이 , 오른쪽 서브노드 최대깊이) + 1 | ||
| private int calMaxDepth(TreeNode node) { | ||
| if (node == null) return 0; | ||
| return Math.max(calMaxDepth(node.left), calMaxDepth(node.right)) + 1; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 새 노드를 매번 생성하는 대신 기존 노드를 연결해도 공간 복잡도가 더 좋아질 수 있습니다. 개선 제안: 고려해볼 만한 대안: 기존 노드를 재활용하여 불필요한 객체 생성 제거. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n+m) | ||
| // SC: O(n+m) | ||
| class Solution { | ||
| public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
| ListNode dummy = new ListNode(0); | ||
| ListNode tail = dummy; | ||
|
|
||
| while (list1 != null && list2 != null) { | ||
| if (list1.val > list2.val) { | ||
| tail.next = new ListNode(list2.val); | ||
| list2 = list2.next; | ||
| } else { | ||
| tail.next = new ListNode(list1.val); | ||
| list1 = list1.next; | ||
| } | ||
| tail = tail.next; | ||
| } | ||
|
|
||
| if (list1 != null) { | ||
| tail.next = list1; | ||
| } | ||
|
|
||
| if (list2 != null) { | ||
| tail.next = list2; | ||
| } | ||
|
|
||
| return dummy.next; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 브루트포스 DFS는 최악의 경우 지수 시간으로 증가할 수 있으나 일반적으로 백트래킹으로 최적화됩니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(m * n * 4^L) | ||
| // SC: O(m * n) | ||
| class Solution { | ||
|
|
||
| private char[][] board; | ||
| private int m, n; | ||
| private boolean[][] visited; | ||
| private char[] word; | ||
| private static final int[] dx = {-1, 1, 0, 0}; | ||
| private static final int[] dy = {0, 0, -1, 1}; | ||
|
|
||
|
|
||
| public boolean exist(char[][] board, String word) { | ||
| this.board = board; | ||
| this.word = word.toCharArray(); | ||
| this.n = board.length; | ||
| this.m = board[0].length; | ||
| this.visited = new boolean[n][m]; | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| for (int j = 0; j < m; j++) { | ||
| if (dfs(i, j, 0)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private boolean dfs(int x, int y, int wordIndex) { | ||
| if (board[x][y] != word[wordIndex] || visited[x][y]) { | ||
| return false; | ||
| } | ||
| if (wordIndex == word.length - 1) { | ||
| return true; | ||
| } | ||
|
|
||
| visited[x][y] = true; | ||
|
|
||
| for (int i = 0; i < 4; i++) { | ||
| int nx = x + dx[i]; | ||
| int ny = y + dy[i]; | ||
| if (nx < 0 || nx >= n || ny < 0 || ny >= m) { | ||
| continue; | ||
| } | ||
|
|
||
| if (board[nx][ny] == word[wordIndex + 1] && !visited[nx][ny]) { | ||
| if (dfs(nx, ny, wordIndex + 1)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| visited[x][y] = false; | ||
| return false; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정답은 배열의 중간 값과 끝 값의 관계를 이용해 왼쪽/오른쪽 구간 중 비정렬 부분으로 이동합니다.
개선 제안: 현재 구현이 적절해 보입니다.