-
-
Notifications
You must be signed in to change notification settings - Fork 195
[krokerdile] WEEK 4 Solutions #1368
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
7 commits
Select commit
Hold shift + click to select a range
e308672
merge-two-sorted-lists solution
krokerdile 0a87ac8
maximum-depth-of-binary-tree solution
krokerdile 23be100
find-minimum solution
krokerdile dd39567
word-search solution
krokerdile 77c39aa
coin-change solution
krokerdile c4c5b47
개행문자 추가
krokerdile 448f741
개행 문자 추가
krokerdile 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,25 @@ | ||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
var coinChange = function(coins, amount) { | ||
const dp = new Array(amount + 1).fill(Infinity); | ||
dp[0] = 0; // 0원을 만들기 위한 동전 수는 0개 | ||
|
||
// bottom-up DP | ||
for (let i = 1; i <= amount; i++) { | ||
for (const coin of coins) { | ||
if (i - coin >= 0) { | ||
dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||
} | ||
} | ||
} | ||
|
||
return dp[amount] === Infinity ? -1 : dp[amount]; | ||
}; | ||
|
||
// 시간복잡도: O(amount * coins.length) | ||
// -> 각 금액 i마다 모든 coin을 시도하기 때문 | ||
// 공간복잡도: O(amount) | ||
// -> dp 배열을 사용하기 때문 |
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,26 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var findMin = function(nums) { | ||
let left = 0; | ||
let right = nums.length - 1; | ||
|
||
while (left < right) { | ||
const mid = Math.floor((left + right) / 2); | ||
|
||
// 중간값이 오른쪽보다 크면 최소값은 오른쪽에 있다! | ||
if (nums[mid] > nums[right]) { | ||
left = mid + 1; | ||
} else { | ||
// 최소값은 mid를 포함한 왼쪽에 있다! | ||
right = mid; | ||
} | ||
} | ||
|
||
// left == right일 때 최소값이 위치함 | ||
return nums[left]; | ||
}; | ||
|
||
// 시간 복잡도: O(log n), 이진 탐색으로 탐색 범위를 절반씩 줄여나감 | ||
// 공간 복잡도: 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,30 @@ | ||
/** | ||
* 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 === null) return 0; | ||
|
||
let queue = [root]; | ||
let depth = 0; | ||
|
||
while (queue.length > 0) { | ||
let levelSize = queue.length; | ||
for (let i = 0; i < levelSize; i++) { | ||
const node = queue.shift(); | ||
if (node.left) queue.push(node.left); | ||
if (node.right) queue.push(node.right); | ||
} | ||
depth++; | ||
} | ||
|
||
return depth; | ||
}; |
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,34 @@ | ||
/** | ||
* 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) { | ||
// 결과 리스트의 시작점을 위한 더미 노드 | ||
let dummy = new ListNode(-1); | ||
let current = dummy; | ||
|
||
// 둘 다 null이 아닐 때까지 반복 | ||
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 dummy.next; // dummy 다음이 진짜 head | ||
}; |
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,47 @@ | ||
/** | ||
* @param {character[][]} board | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
var exist = function(board, word) { | ||
const rows = board.length; | ||
const cols = board[0].length; | ||
|
||
// DFS 함수 정의 | ||
const dfs = (r, c, idx) => { | ||
// 단어 끝까지 찾은 경우 | ||
if (idx === word.length) return true; | ||
|
||
// 범위 밖이거나, 문자 불일치거나, 이미 방문한 경우 | ||
if ( | ||
r < 0 || c < 0 || r >= rows || c >= cols || | ||
board[r][c] !== word[idx] | ||
) { | ||
return false; | ||
} | ||
|
||
const temp = board[r][c]; // 현재 문자 저장 | ||
board[r][c] = "#"; // 방문 표시 | ||
|
||
// 상하좌우로 탐색 | ||
const found = dfs(r + 1, c, idx + 1) || | ||
dfs(r - 1, c, idx + 1) || | ||
dfs(r, c + 1, idx + 1) || | ||
dfs(r, c - 1, idx + 1); | ||
|
||
board[r][c] = temp; // 백트래킹: 원상복구 | ||
|
||
return found; | ||
}; | ||
|
||
// 보드의 모든 칸에서 시작해보기 | ||
for (let r = 0; r < rows; r++) { | ||
for (let c = 0; c < cols; c++) { | ||
if (dfs(r, c, 0)) return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
// 시간복잡도: O(m * n * 4^L), m*n번 DFS 시작 가능하고, 각 DFS는 최대 4방향 * 단어 길이 만큼 탐색 |
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 자체를 그대로 사용하시고 복구시키면서 공간 복잡도를 최적화 하신게 너무 좋습니다!