-
-
Notifications
You must be signed in to change notification settings - Fork 362
[Yiseull] WEEK 04 Solutions #2755
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
d11e0ca
70cd614
c5df935
03671de
58eee0d
0a619fc
9386a4d
2b61f47
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,22 @@ | ||
| class Solution { | ||
|
|
||
| private int answer = Integer.MAX_VALUE; | ||
|
|
||
| public int coinChange(int[] coins, int amount) { | ||
| final int IMPOSSIBLE = amount + 1; | ||
|
|
||
| int[] dp = new int[amount + 1]; | ||
| Arrays.fill(dp, IMPOSSIBLE); | ||
| dp[0] = 0; | ||
|
|
||
| for (int i = 1; i < amount + 1; i++) { | ||
| for (int coin : coins) { | ||
| if (coin <= i) { | ||
| dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return dp[amount] == IMPOSSIBLE ? -1 : dp[amount]; | ||
| } | ||
|
Comment on lines
+8
to
+21
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.
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,21 @@ | ||
| class Solution { | ||
| public int findMin(int[] nums) { | ||
| return binarySearch(nums); | ||
| } | ||
|
|
||
| // 공간복잡도: O(1), 시간복잡도: O(logn) | ||
| private int binarySearch(int[] nums) { | ||
| int left = 0, right = nums.length - 1; | ||
|
|
||
| while (left < right) { | ||
| if (nums[left] <= nums[right]) return nums[left]; | ||
|
|
||
| int mid = (left + right) / 2; | ||
|
|
||
| if (nums[right] < nums[mid]) 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 노드 방문은 한 번씩 이루어지며, 재귀 스택은 트리의 높이 h 만큼 사용된다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * 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 int maxDepth(TreeNode root) { | ||
| return dfs(root); | ||
| } | ||
|
|
||
| private int dfs(TreeNode node) { | ||
| if (node == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| return 1 + Math.max(dfs(node.left), dfs(node.right)); | ||
| } | ||
| } |
|
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 @@ | ||
| /** | ||
| * 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 mergeTwoLists(ListNode list1, ListNode list2) { | ||
| ListNode head = new ListNode(-1); | ||
| ListNode current = head; | ||
|
|
||
| while (list1 != null && list2 != null) { | ||
| if (list1.val <= list2.val) { | ||
| current.next = list1; | ||
| list1 = list1.next; | ||
| } else { | ||
| current.next = list2; | ||
| list2 = list2.next; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| current.next = (list1 != null) ? list1 : list2; | ||
|
|
||
| return head.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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 최악의 경우 모든 경로를 탐색하므로 지수적 시간 복잡도가 된다. 중복 방문 방지를 위해 방문 흔적을 남긴다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| class Solution { | ||
|
|
||
| private final int[] dx = {-1, 1, 0, 0}; | ||
| private final int[] dy = {0, 0, -1, 1}; | ||
|
|
||
| public boolean exist(char[][] board, String word) { | ||
| for (int i = 0; i < board.length; i++) { | ||
| for (int j = 0; j < board[0].length; j++) { | ||
| if (board[i][j] == word.charAt(0) && dfs(board, word, i, j, 1)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private boolean dfs(char[][] board, String word, int x, int y, int index) { | ||
| if (index == word.length()) return true; | ||
|
|
||
| char tmp = board[x][y]; | ||
| board[x][y] = '0'; | ||
|
|
||
| for (int i = 0; i < 4; i++) { | ||
| int nextX = x + dx[i], nextY = y + dy[i]; | ||
| if (0 <= nextX && nextX < board.length && 0 <= nextY && nextY < board[0].length && board[nextX][nextY] == word.charAt(index)) { | ||
| if(dfs(board, word, nextX, nextY, index + 1)) return true; | ||
| } | ||
| } | ||
|
|
||
| board[x][y] = tmp; | ||
|
|
||
| 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[i]를 갱신한다. dp 배열 크기는 amount+1 이고, 외부 루프가 amount 번, 내부가 동전 수(C) 만큼 수행된다.
개선 제안: 현재 구현이 적절해 보입니다.