-
-
Notifications
You must be signed in to change notification settings - Fork 305
[casentino] WEEK 04 Solutions #2133
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| function coinChange(coins: number[], amount: number): number { | ||
| const dp = new Array(amount + 1).fill(amount); | ||
| dp[0] = 0; | ||
| for (let i = 1; i <= amount; i++) { | ||
| for (let j = 0; j < coins.length; j++) { | ||
| if (i - coins[j] >= 0) { | ||
| dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]); | ||
| } | ||
| } | ||
| } | ||
| return dp[amount] === Number.POSITIVE_INFINITY ? -1 : dp[amount]; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| function findMin(nums: number[]): number { | ||
| let left = 0; | ||
| let right = nums.length - 1; | ||
|
|
||
| while (left < right) { | ||
| const middle = Math.floor((left + right) / 2); | ||
|
|
||
| if (nums[middle] < nums[right]) { | ||
| right = middle; | ||
| } else { | ||
| left = middle + 1; | ||
| } | ||
| } | ||
| return nums[left]; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * class TreeNode { | ||
| * val: number | ||
| * left: TreeNode | null | ||
| * right: TreeNode | null | ||
| * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.left = (left===undefined ? null : left) | ||
| * this.right = (right===undefined ? null : right) | ||
| * } | ||
| * } | ||
| */ | ||
|
|
||
| function maxDepth(root: TreeNode | null): number { | ||
| function recursive(node: TreeNode | null, count: number) { | ||
| if (!node) { | ||
| return count; | ||
| } | ||
| const left = recursive(node.left, count + 1); | ||
| const right = recursive(node.right, count + 1); | ||
| return left > right ? left : right; | ||
| } | ||
| return recursive(root, 0); | ||
| } | ||
|
Comment on lines
+15
to
+25
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. 알고리즘을 아래처럼 이해했는데, 맞게 이해했다면 좋은 로직이네요!
현재 함수는 예를 들어 노드가 없을 때 0을 반환하고, 이렇게 하면 재귀 함수의 매개변수를 줄일 수 있어요! 😊
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. 그러네요! 말씀해주신 방법으로도 풀어봐야겠어요! 조언 감사드려요 🙇♂️ |
||
|
|
||
| class TreeNode { | ||
| val: number; | ||
| left: TreeNode | null; | ||
| right: TreeNode | null; | ||
| constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
| this.val = val === undefined ? 0 : val; | ||
| this.left = left === undefined ? null : left; | ||
| this.right = right === undefined ? null : right; | ||
| } | ||
| } | ||
|
Comment on lines
+27
to
+36
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. 타입스크립트 컴파일 에러 나길래 선언만 해두었어요! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * class ListNode { | ||
| * val: number | ||
| * next: ListNode | null | ||
| * constructor(val?: number, next?: ListNode | null) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.next = (next===undefined ? null : next) | ||
| * } | ||
| * } | ||
| */ | ||
|
|
||
| function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { | ||
| if (!list1 || !list2) { | ||
| return list1 ?? list2; | ||
| } | ||
| function merge(node1: ListNode | null, node2: ListNode | null) { | ||
| if (!node1 || !node2) { | ||
| return node1 ?? node2; | ||
| } | ||
| if (node1.val < node2.val) { | ||
| node1.next = merge(node1.next, node2); | ||
| } | ||
| if (node1.val >= node2.val) { | ||
| node2.next = merge(node1, node2.next); | ||
| } | ||
|
|
||
| return node1.val < node2.val ? node1 : node2; | ||
| } | ||
| return merge(list1, list2); | ||
| } | ||
|
Comment on lines
+13
to
+31
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. 재귀로도 이렇게 깔끔한 풀이가 가능하네요! 😮 node1.val < node2.val 기준으로 next를 재귀적으로 이어붙이는 구조가 명확하게 보여서 로직이 한눈에 보이네요!! 저는 재귀가 약해서 이해하는 데 조금 시간이 걸렸는데요 😅 다음에는 참고해서 풀어봐야겠어요!
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. 저도 재귀는 아직 구현하면서도 햇갈려서 조건주는게 많이 어색한것같아요 ㅠ 더 많이 풀어봐야할것같아요! |
||
|
|
||
| class ListNode { | ||
| val: number; | ||
| next: ListNode | null; | ||
| constructor(val?: number, next?: ListNode | null) { | ||
| this.val = val === undefined ? 0 : val; | ||
| this.next = next === undefined ? null : next; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| function exist(board: string[][], word: string): boolean { | ||
| const m = board.length; | ||
| const n = board[0].length; | ||
|
|
||
| const visited = Array.from({ length: m }, () => new Array(n).fill(0)); | ||
| function findDirection(currentI: number, currentJ: number, findNextIdx: number) { | ||
| if (findNextIdx === word.length) { | ||
| return true; | ||
| } | ||
| if ( | ||
| currentI < 0 || | ||
| currentJ < 0 || | ||
| currentI >= m || | ||
| currentJ >= n || | ||
| board[currentI][currentJ] !== word[findNextIdx] | ||
| ) { | ||
| return false; | ||
| } | ||
| if (visited[currentI][currentJ] === 1) { | ||
| return false; | ||
| } | ||
| visited[currentI][currentJ] = 1; | ||
|
|
||
| const isApproachLastWord = | ||
| findDirection(currentI + 1, currentJ, findNextIdx + 1) || | ||
| findDirection(currentI - 1, currentJ, findNextIdx + 1) || | ||
| findDirection(currentI, currentJ - 1, findNextIdx + 1) || | ||
| findDirection(currentI, currentJ + 1, findNextIdx + 1); | ||
|
|
||
| if (!isApproachLastWord) { | ||
| visited[currentI][currentJ] = 0; | ||
| } | ||
| return isApproachLastWord; | ||
| } | ||
| for (let i = 0; i < m; i++) { | ||
| for (let j = 0; j < n; j++) { | ||
| const result = findDirection(i, j, 0); | ||
| if (result) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| 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.
보기 좋은 이분 탐색 코드네요 👍