-
-
Notifications
You must be signed in to change notification settings - Fork 362
[seueooo] WEEK 04 Solutions #2771
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
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 수행 시 각 합을 한 번만 처리하도록 하여 최단 경로를 찾는다. 방문 중복 제거를 위해 Set를 사용한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * bfs | ||
| * 시간 복잡도: O(amount * n) | ||
| * 공간 복잡도: O(amount) | ||
| * @param {number[]} coins | ||
| * @param {number} amount | ||
| * @return {number} | ||
| */ | ||
| var coinChange = function (coins, amount) { | ||
| let q = []; | ||
| let visited = new Set(); | ||
| q.push([0, 0]); | ||
| while (q.length) { | ||
| const [sum, count] = q.shift(); | ||
| if (sum === amount) return count; | ||
| if (visited.has(sum)) continue; | ||
| visited.add(sum); | ||
| for (let i = 0; i < coins.length; i++) { | ||
| const nextSum = sum + coins[i]; | ||
| if (nextSum <= amount) { | ||
| q.push([nextSum, count + 1]); | ||
| } | ||
| } | ||
| } | ||
| return -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,22 @@ | ||
| /** | ||
| 이분 탐색 | ||
| 시간 복잡도 : O(log n) | ||
| 공간 복잡도 : O(1) | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var findMin = function (nums) { | ||
| let left = 0; | ||
| let right = nums.length - 1; | ||
|
|
||
| while (left < right) { | ||
| let mid = Math.floor((left + right) / 2); | ||
| if (nums[mid] > nums[right]) { | ||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트리의 모든 노드를 한 번씩 방문하고 각 재귀 호출이 깊이만큼 스택을 사용한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /** | ||
| 재귀 | ||
| * Definition for a binary tree node. | ||
| * function TreeNode(val, left, right) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.left = (left===undefined ? null : left) | ||
| * this.right = (right===undefined ? null : right) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {TreeNode} root | ||
| * @return {number} | ||
| */ | ||
| var maxDepth = function (root) { | ||
| if (!root) return 0; | ||
| return 1 + Math.max(maxDepth(root.left), maxDepth(root.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. 재귀로 풀어주셨네요. 재귀 아닌 방식으로 풀어보셔도 재밌으실 거 같습니다.
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 @@ | ||
| /** | ||
| * 풀이 | ||
| * 더 작은 노드를 머리로 두고, 그 next에 나머지를 병합한 결과를 연결. | ||
| * 최종적으로 머리를 반환 | ||
| * | ||
| * 시간복잡도 - O(n + m) : n, m은 각각 list1, list2의 길이 | ||
| * 공간복잡도 - O(n + m) : 재귀 호출 스택 | ||
| * | ||
| * Definition for singly-linked list. | ||
| * function ListNode(val, next) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.next = (next===undefined ? null : next) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {ListNode} list1 | ||
| * @param {ListNode} list2 | ||
| * @return {ListNode} | ||
| */ | ||
| var mergeTwoLists = function (list1, list2) { | ||
| if (!list1) return list2; | ||
| if (!list2) return list1; | ||
|
|
||
| if (list1.val <= list2.val) { | ||
| list1.next = mergeTwoLists(list1.next, list2); | ||
| return list1; | ||
| } else { | ||
| list2.next = mergeTwoLists(list1, list2.next); | ||
| return list2; | ||
| } | ||
| }; |
|
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
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,56 @@ | ||
| /** | ||
| 현재 경로를 하나 선택해서 끝까지 진행하고, 실패하면 방문 표시를 되돌려 다른 경로를 시도 | ||
| bfs, 백트래킹 | ||
| * @param {character[][]} board | ||
| * @param {string} word | ||
| * @return {boolean} | ||
| */ | ||
| var exist = function (board, word) { | ||
| const rows = board.length; | ||
| const cols = board[0].length; | ||
|
|
||
| const dx = [-1, 1, 0, 0]; | ||
| const dy = [0, 0, -1, 1]; | ||
|
|
||
| function dfs(x, y, index) { | ||
| if (board[y][x] !== word[index]) { | ||
| return false; | ||
| } | ||
|
|
||
| if (index === word.length - 1) { | ||
| return true; | ||
| } | ||
|
|
||
| // 방문 처리 | ||
| const original = board[y][x]; | ||
| board[y][x] = "#"; | ||
|
|
||
| for (let i = 0; i < 4; i++) { | ||
| const nx = x + dx[i]; | ||
| const ny = y + dy[i]; | ||
|
|
||
| if ( | ||
| nx >= 0 && | ||
| nx < cols && | ||
| ny >= 0 && | ||
| ny < rows && | ||
| board[ny][nx] !== "#" | ||
| ) { | ||
| if (dfs(nx, ny, index + 1)) { | ||
| board[y][x] = original; | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| board[y][x] = original; | ||
| return false; | ||
| } | ||
|
|
||
| for (let i = 0; i < rows; i++) { | ||
| for (let j = 0; j < cols; j++) { | ||
| if (dfs(j, i, 0)) return true; | ||
| } | ||
| } | ||
| return false; | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.
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.
요건 돌려보면 runtime이 5% 정도 나오는데 최적화 시도해보시면 좋을 거 같습니다.