-
-
Notifications
You must be signed in to change notification settings - Fork 195
[uraflower] WEEK 04 solutions #1346
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a6e2de7
[ PS ] : Merge Two Sorted Lists
uraflower 8174793
[ PS ] : Maximum Depth of Binary Tree
uraflower 26cb970
[ PS ] : Find Minimum in Rotated Sorted Array
uraflower 77ed47a
Create uraflower.js
uraflower 12117a4
[ PS ] : Coin Change
uraflower File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 주어진 금액을 동전으로 거스를 때 최소 동전 개수를 반환하는 함수 | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
const coinChange = function (coins, amount) { | ||
const dp = [0, ...Array(amount).fill(amount + 1)]; | ||
|
||
for (let coin of coins) { | ||
for (let n = coin; n <= amount; n++) { | ||
dp[n] = Math.min(dp[n], dp[n - coin] + 1) | ||
} | ||
} | ||
|
||
return dp[amount] > amount ? -1 : dp[amount] | ||
}; | ||
|
||
// 시간복잡도: O(c*n) (c: coins.length, n: amount) | ||
// 공간복잡도: O(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* 주어진 배열의 최솟값을 반환하는 함수 | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
// 첫 번째 시도 | ||
const findMin = function(nums) { | ||
return Math.min(...nums); | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) | ||
|
||
// =========================================== | ||
// 두 번째 시도 | ||
const findMin = function(nums) { | ||
let left = 0; | ||
let right = nums.length - 1; | ||
let mid; | ||
|
||
while (left < right) { | ||
mid = Math.floor((left + right) / 2); | ||
|
||
if (nums[mid] < nums[right]) { | ||
right = mid; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
|
||
return nums[left]; | ||
} | ||
|
||
// 시간복잡도: O(logn) | ||
// 공간복잡도: O(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 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} | ||
*/ | ||
const maxDepth = function(root) { | ||
let maxDepth = 0; | ||
|
||
function bfs(node, depth) { | ||
if (!node) return; | ||
|
||
depth += 1; | ||
if (maxDepth < depth) maxDepth = depth; | ||
bfs(node.left, depth); | ||
bfs(node.right, depth); | ||
} | ||
|
||
bfs(root, maxDepth); | ||
return maxDepth; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(h) (h: 트리의 높이. 최악의 경우 편향트리일 때 h===n으로 O(n)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* 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} | ||
*/ | ||
const mergeTwoLists = function (list1, list2) { | ||
let head = new ListNode(-1, null); | ||
let mergedList = head; | ||
|
||
let node1 = list1; | ||
let node2 = list2; | ||
|
||
while (node1 && node2) { | ||
if (node1.val >= node2.val) { | ||
mergedList.next = node2; | ||
node2 = node2.next; | ||
} else if (node1.val < node2.val) { | ||
mergedList.next = node1; | ||
node1 = node1.next; | ||
} | ||
|
||
mergedList = mergedList.next; | ||
} | ||
|
||
mergedList.next = node1 ?? node2; | ||
|
||
return head.next; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* 주어진 2차 배열에 word가 있는지 여부를 반환하는 함수 | ||
* @param {character[][]} board | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
const exist = function (board, word) { | ||
const row = board.length; | ||
const col = board[0].length; | ||
const visited = Array.from({ length: row }, () => Array(col).fill(false)); | ||
const dr = [0, 0, 1, -1]; | ||
const dc = [1, -1, 0, 0]; | ||
|
||
function dfs(r, c, charIdx) { | ||
|
||
if (charIdx + 1 === word.length) return true; | ||
|
||
for (let i = 0; i < 4; i++) { | ||
const nr = r + dr[i]; | ||
const nc = c + dc[i]; | ||
|
||
if (0 <= nr && nr < row && 0 <= nc && nc < col && !visited[nr][nc] && word[charIdx + 1] === board[nr][nc]) { | ||
visited[nr][nc] = true; | ||
if (dfs(nr, nc, charIdx + 1)) return true; | ||
visited[nr][nc] = false; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
for (let r = 0; r < row; r++) { | ||
for (let c = 0; c < col; c++) { | ||
if (word[0] === board[r][c]) { | ||
visited[r][c] = true; | ||
if (dfs(r, c, 0)) return true; | ||
visited[r][c] = false; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
// 시간복잡도: O(m * n * 4^L) (L = word.length. 네 방향으로 최대 L만큼 수행할 수 있음.) | ||
// 공간복잡도: O(m * n) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
지금 풀이도 충분히 좋은데, 참고로 board를 임시로 마킹하는 방법으로 공간 복잡도를 줄일 수도 있어서 가볍게 고려해보셔도 좋을 것 같아요. 한 주 동안 고생 많으셨습니다. 👍