-
-
Notifications
You must be signed in to change notification settings - Fork 362
[jaekwang97] WEEK 04 Solutions #2751
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
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,20 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| class Solution { | ||
| public int coinChange(int[] coins, int amount) { | ||
| int[] dp = new int[amount + 1]; | ||
|
|
||
| Arrays.fill(dp, amount + 1); | ||
| dp[0] = 0; | ||
|
|
||
| for (int i = 1; i <= amount; i++) { | ||
| for (int coin : coins) { | ||
| if (i - coin >= 0) { | ||
| dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return dp[amount] == amount + 1 ? -1 : dp[amount]; | ||
|
Comment on lines
+5
to
+18
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. amount + 1 이 매직넘버라 선언해서 사용하시면 좋을 거 같습니다.
Contributor
Author
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. 감사합니다. 상수 처리하면 가독성이 더 좋아질 것 같네요! |
||
| } | ||
| } | ||
|
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,18 @@ | ||
| class Solution { | ||
| public int findMin(int[] nums) { | ||
| int left = 0; | ||
| int right = nums.length - 1; | ||
|
|
||
| while (left < right) { | ||
| int mid = left + (right - left) / 2; | ||
|
|
||
| if (nums[mid] > nums[right]) { | ||
|
Comment on lines
+6
to
+9
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. 탐색 중간에 답인 경우 리턴이 가능해서 최적화 해보시면 좋을 거 같습니다. |
||
| left = mid + 1; | ||
| } else { | ||
| right = mid; | ||
| } | ||
| } | ||
|
|
||
| return nums[left]; | ||
| } | ||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 전역 max 변수를 사용해 현재 깊이를 추적하며 재귀적으로 자식 노드를 탐색한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| class Solution { | ||
| private int max; | ||
|
|
||
| public int maxDepth(TreeNode root) { | ||
| if (root == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| max = 0; | ||
| dfs(root, 1); | ||
|
|
||
| return max; | ||
| } | ||
|
|
||
| private void dfs(TreeNode node, int depth) { | ||
| if (node == null) { | ||
| return; | ||
| } | ||
|
|
||
| max = Math.max(max, depth); | ||
|
|
||
| dfs(node.left, depth + 1); | ||
| dfs(node.right, depth + 1); | ||
|
Comment on lines
+2
to
+23
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 리턴 값을 사용해보셔도 좋을 거 같습니다. |
||
| } | ||
| } | ||
|
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,32 @@ | ||
| class Solution { | ||
| public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
| ListNode dummy = new ListNode(); | ||
| ListNode tail = dummy; | ||
|
|
||
| while (list1 != null && list2 != null) { | ||
| if (list1.val <= list2.val) { | ||
| tail.next = new ListNode(list1.val); | ||
| list1 = list1.next; | ||
| } else { | ||
| tail.next = new ListNode(list2.val); | ||
| list2 = list2.next; | ||
| } | ||
|
|
||
| tail = tail.next; | ||
| } | ||
|
|
||
| while (list1 != null) { | ||
| tail.next = new ListNode(list1.val); | ||
| tail = tail.next; | ||
| list1 = list1.next; | ||
| } | ||
|
|
||
| while (list2 != null) { | ||
| tail.next = new ListNode(list2.val); | ||
| tail = tail.next; | ||
| list2 = list2.next; | ||
|
Comment on lines
+7
to
+27
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. ListNode 를 새로 만들지 않고 기존 노드만을 이용해서 풀어보셔도 좋을 거 같습니다. |
||
| } | ||
|
|
||
| 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를 수행하고, 방문 표식을 이용해 중복 방문을 방지한다. 개선 제안: 현재 구현이 적절해 보입니다.
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. 요거 돌려보니 runtime 50% 정도 나와서 최적화 좀 더 해보셔도 좋을 거 같습니다.
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. 앞으로 풀이할 때 Beats도 같이 봐아겠네요. 백분위였군요👍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| class Solution { | ||
|
|
||
| static int[] dx = {0, 1, 0, -1}; | ||
| static int[] dy = {1, 0, -1, 0}; | ||
| static int n, m; | ||
|
|
||
| public boolean exist(char[][] board, String word) { | ||
| m = board[0].length; | ||
| n = board.length; | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| for (int j = 0; j < m; j++) { | ||
| if (board[i][j] != word.charAt(0)) continue; | ||
|
|
||
| char cur = board[i][j]; | ||
| board[i][j] = '#'; | ||
| boolean flag = dfs(board, word, 1, i, j); | ||
| board[i][j] = cur; | ||
|
|
||
| if (flag) return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static boolean dfs( | ||
| char[][] board, | ||
| String word, | ||
| int idx, | ||
| int x, | ||
| int y | ||
| ) { | ||
| if (idx == word.length()) return 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.charAt(idx)) continue; | ||
|
|
||
| char cur = board[nx][ny]; | ||
| board[nx][ny] = '#'; | ||
|
|
||
| boolean flag = dfs(board, word, idx + 1, nx, ny); | ||
|
|
||
| board[nx][ny] = cur; | ||
|
|
||
| if (flag) return true; | ||
| } | ||
|
|
||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 동전 종류를 모두 순회하며 각 합에 대해 최솟값을 갱신한다. dp 배열 크기는 amount+1 이므로 필요 공간은 선형이다.
개선 제안: 현재 구현이 적절해 보입니다.