-
-
Notifications
You must be signed in to change notification settings - Fork 247
[jun0811] WEEK 04 Solutions #1821
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
Open
jun0811
wants to merge
6
commits into
DaleStudy:main
Choose a base branch
from
jun0811:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
498a1d2
merge-two-sorted-lists
jun0811 4451bf8
Maximum Depth of Binary Tree
jun0811 1507910
find-minimum-in-rotated-sorted-array
jun0811 f77974a
maximum-depth-of-binary-tree
jun0811 f1f7933
word-search
jun0811 7ca169f
coin-change
jun0811 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,13 @@ | ||
var coinChange = function(coins, amount) { | ||
if(amount == 0) return 0 | ||
|
||
const dp = [0, ...new Array(amount).fill(amount+1)] | ||
|
||
for (const coin of coins) { | ||
for (let i = coin; i <=amount; i++) { | ||
dp[i] = Math.min(dp[i], dp[i-coin] + 1) | ||
} | ||
} | ||
|
||
return dp[amount] < amount+1 ? dp[amount] : -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,16 @@ | ||
var findMin = function (nums) { | ||
let left = 0, | ||
right = nums.length - 1; | ||
|
||
while (left < right) { | ||
const mid = Math.floor((left + right) / 2); | ||
|
||
if (nums[mid] > nums[right]) { | ||
left = mid + 1; // 최솟값이 오른쪽에 있음 | ||
} else { | ||
right = mid; // 최솟값이 왼쪽에 있음 (mid 포함) | ||
} | ||
} | ||
|
||
return nums[left]; | ||
}; |
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,22 @@ | ||
var maxDepth = function (root) { | ||
let res = 0; | ||
|
||
if (!root) return res; // root 자체가 없을 경우만 0 | ||
|
||
function check(node, depth) { | ||
if (!node.left && !node.right) { | ||
res = Math.max(res, depth); | ||
return; | ||
} | ||
|
||
if (node.left) { | ||
check(node.left, depth + 1); | ||
} | ||
if (node.right) { | ||
check(node.right, depth + 1); | ||
} | ||
} | ||
|
||
check(root, 1); // 루트부터 시작 | ||
return res; | ||
}; |
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 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
|
||
var mergeTwoLists = function (list1, list2) { | ||
if (!list1) return list2; | ||
else 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; | ||
} | ||
}; |
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,58 @@ | ||
/** | ||
* @param {character[][]} board | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
|
||
const directions = [ | ||
[0, -1], | ||
[1, 0], | ||
[-1, 0], | ||
[0, 1], | ||
]; | ||
|
||
var exist = function (board, word) { | ||
const cols = board[0].length; // 가로 (열 개수) | ||
const rows = board.length; // 세로 (행 개수) | ||
let res = false; | ||
|
||
for (let col = 0; col < cols; col++) { | ||
for (let row = 0; row < rows; row++) { | ||
if (board[row][col] != word[0]) continue; | ||
|
||
const visited = Array.from({ length: rows }, () => | ||
Array(cols).fill(false) | ||
); | ||
if (res) break; | ||
dfs(row, col, board[row][col], visited); | ||
} | ||
} | ||
|
||
function check(row, col) { | ||
if (!(row >= 0 && row < rows)) return false; | ||
if (!(col >= 0 && col < cols)) return false; | ||
return true; | ||
} | ||
|
||
function dfs(row, col, str, visited) { | ||
if (str == word) { | ||
res = true; | ||
return; | ||
} | ||
if (str.length >= word.length) return; | ||
visited[row][col] = true; | ||
|
||
for (const direction of directions) { | ||
const [d_r, d_c] = direction; | ||
const newCol = col + d_c; | ||
const newRow = row + d_r; | ||
|
||
if (check(newRow, newCol) && !visited[newRow][newCol]) { | ||
dfs(newRow, newCol, str + board[newRow][newCol], visited); | ||
} | ||
} | ||
visited[row][col] = false; | ||
} | ||
|
||
return res; | ||
}; |
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.
상단 부터 이런 '예외 처리(?)' 좋은 것 같습니다!