-
-
Notifications
You must be signed in to change notification settings - Fork 195
[HoonDongKang] Week 10 Solutions #1548
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
247014d
add Invert Binary Tree solution
HoonDongKang f6e471c
add Search in Rotated Sorted Array solution
HoonDongKang 9b4dd42
add Course Schedule solution
HoonDongKang 4db8165
add Jump Game solution
HoonDongKang 97e10ba
add Merge k Sorted Lists solution
HoonDongKang 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,42 @@ | ||
/** | ||
* [Problem]: [207] Course Schedule | ||
* (https://leetcode.com/problems/course-schedule/description/) | ||
*/ | ||
|
||
function canFinish(numCourses: number, prerequisites: number[][]): boolean { | ||
// 시간복잡도 O(n+m) | ||
// 공간복잡도 O(n+m) | ||
function graphFunc(numCourses: number, prerequisites: number[][]): boolean { | ||
const graph: number[][] = Array.from({ length: numCourses }, () => []); | ||
|
||
for (const [course, prerequisite] of prerequisites) { | ||
graph[prerequisite].push(course); | ||
} | ||
|
||
let traversing = new Array(numCourses).fill(false); | ||
let visited = new Array(numCourses).fill(false); | ||
|
||
function dfs(course: number): boolean { | ||
if (traversing[course]) return false; | ||
if (visited[course]) return true; | ||
|
||
traversing[course] = true; | ||
for (let pre of graph[course]) { | ||
if (!dfs(pre)) { | ||
return false; | ||
} | ||
} | ||
|
||
traversing[course] = false; | ||
visited[course] = true; | ||
return true; | ||
} | ||
|
||
for (let i = 0; i < numCourses; i++) { | ||
if (!visited[i] && !dfs(i)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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,53 @@ | ||
/** | ||
* [Problem]: [226] Invert Binary Tree | ||
* (https://leetcode.com/problems/invert-binary-tree/) | ||
*/ | ||
|
||
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 invertTree(root: TreeNode | null): TreeNode | null { | ||
// 시간복잡도 O(n) | ||
// 공간복잡도 O(n) | ||
function recursiveFunc(root: TreeNode | null): TreeNode | null { | ||
if (root === null) { | ||
return null; | ||
} | ||
|
||
const temp = root.left; | ||
root.left = invertTree(root.right); | ||
root.right = invertTree(temp); | ||
|
||
return root; | ||
} | ||
// 시간복잡도 O(n) | ||
// 공간복잡도 O(n) | ||
function stackFunc(root: TreeNode | null): TreeNode | null { | ||
if (root === null) { | ||
return null; | ||
} | ||
|
||
const stack: Array<TreeNode | null> = [root]; | ||
|
||
while (stack.length > 0) { | ||
const node = stack.pop()!; | ||
|
||
if (node === null) { | ||
continue; | ||
} | ||
|
||
[node.left, node.right] = [node.right, node.left]; | ||
stack.push(node.left, node.right); | ||
} | ||
|
||
return root; | ||
} | ||
} |
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,71 @@ | ||
/** | ||
* [Problem]: [55] Jump Game | ||
* (https://leetcode.com/problems/jump-game/description/) | ||
*/ | ||
function canJump(nums: number[]): boolean { | ||
// 시간복잡도 O(N^N) | ||
// 공간복잡도 O(N) | ||
// Time Limit Exceeded | ||
function dfsFunc(nums: number[]): boolean { | ||
function dfs(start: number): boolean { | ||
if (start === nums.length - 1) return true; | ||
for (let i = 1; i <= nums[start]; i++) { | ||
if (dfs(start + i)) return true; | ||
} | ||
return false; | ||
} | ||
return dfs(0); | ||
} | ||
// 시간복잡도 O(N^2) | ||
// 공간복잡도 O(N) | ||
function dpFunc(nums: number[]): boolean { | ||
const n = nums.length; | ||
const dp = Array(n).fill(false); | ||
dp[0] = true; | ||
|
||
for (let i = 1; i < n; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (dp[j] && j + nums[j] >= i) { | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dp[n - 1]; | ||
} | ||
// 시간복잡도 O(N^2) | ||
// 공간복잡도 O(N) | ||
function memoFunc(nums: number[]): boolean { | ||
let memo = new Map<number, boolean>(); | ||
|
||
function dfs(start: number): boolean { | ||
if (start === nums.length - 1) return true; | ||
if (memo.has(start)) return memo.get(start)!; | ||
for (let i = 1; i <= nums[start]; i++) { | ||
if (dfs(start + i)) { | ||
memo.set(start, true); | ||
return true; | ||
} | ||
} | ||
|
||
memo.set(start, false); | ||
return false; | ||
} | ||
|
||
return dfs(0); | ||
} | ||
|
||
// 시간복잡도 O(N) | ||
// 공간복잡도 O(1) | ||
function greedyFunc(nums: number[]): boolean { | ||
let reach = 0; | ||
for (let i = 0; i < nums.length; i++) { | ||
if (i <= reach) { | ||
reach = Math.max(reach, i + nums[i]); | ||
} | ||
} | ||
|
||
return nums.length - 1 <= reach; | ||
} | ||
} |
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. 한 문제를 풀이함에 있어 다양한 방식으로 고민하신 흔적이 보여 굉장히 인상 깊습니다. |
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,101 @@ | ||
/** | ||
* [Problem]: [23] Merge k Sorted Lists | ||
* (https://leetcode.com/problems/merge-k-sorted-lists/description/) | ||
*/ | ||
|
||
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 mergeKLists(lists: Array<ListNode | null>): ListNode | null { | ||
//시간복잡도: O(n log n) | ||
//공간복잡도: O(n) | ||
function bruteForceFunc(lists: Array<ListNode | null>): ListNode | null { | ||
const arr: number[] = []; | ||
let dummy = new ListNode(0); | ||
let current = dummy; | ||
|
||
if (!lists.length) return null; | ||
|
||
for (let i = 0; i < lists.length; i++) { | ||
let currentNode = lists[i]; | ||
while (currentNode) { | ||
arr.push(currentNode.val); | ||
currentNode = currentNode.next; | ||
} | ||
} | ||
|
||
arr.sort((a, b) => a - b); | ||
|
||
for (let j = 0; j < arr.length; j++) { | ||
current.next = new ListNode(arr[j]); | ||
current = current.next; | ||
} | ||
|
||
return dummy.next; | ||
} | ||
|
||
//시간복잡도: O(nk) | ||
//공간복잡도: O(n) | ||
function mergeFunc(lists: Array<ListNode | null>): ListNode | null { | ||
let dummy = new ListNode(0); | ||
let cur = dummy; | ||
|
||
while (lists.some((node) => node !== null)) { | ||
let minVal = Infinity; | ||
let minIdx = -1; | ||
|
||
for (let i = 0; i < lists.length; i++) { | ||
if (lists[i] && lists[i]!.val < minVal) { | ||
minVal = lists[i]!.val; | ||
minIdx = i; | ||
} | ||
} | ||
|
||
if (minIdx !== -1) { | ||
cur.next = new ListNode(minVal); | ||
cur = cur.next; | ||
lists[minIdx] = lists[minIdx]!.next; | ||
} | ||
} | ||
|
||
return dummy.next; | ||
} | ||
//시간복잡도: O(n log k) | ||
//공간복잡도: O(log k) | ||
function divideAndConqureFunc(lists: Array<ListNode | null>): ListNode | null { | ||
if (!lists.length) return null; | ||
if (lists.length === 1) return lists[0]; | ||
|
||
const mid = Math.floor(lists.length / 2); | ||
const left = divideAndConqureFunc(lists.slice(0, mid)); | ||
const right = divideAndConqureFunc(lists.slice(mid)); | ||
|
||
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { | ||
const dummy = new ListNode(-1); | ||
let cur = dummy; | ||
|
||
while (list1 && list2) { | ||
if (list1.val < list2.val) { | ||
cur.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
cur.next = list2; | ||
list2 = list2.next; | ||
} | ||
|
||
cur = cur.next; | ||
} | ||
|
||
cur.next = list1 ?? list2; | ||
return dummy.next; | ||
} | ||
|
||
return mergeTwoLists(left, right); | ||
} | ||
} |
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,32 @@ | ||
/** | ||
* [Problem]: [33] Search in Rotated Sorted Array | ||
* (https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | ||
*/ | ||
function search(nums: number[], target: number): number { | ||
//시간복잡도 O(log n) | ||
//공간복잡도 O(1) | ||
let left = 0; | ||
let right = nums.length - 1; | ||
|
||
while (left <= right) { | ||
let mid = Math.floor((left + right) / 2); | ||
|
||
if (nums[mid] === target) return mid; | ||
|
||
if (nums[left] <= nums[mid]) { | ||
if (nums[left] <= target && target < nums[mid]) { | ||
right = mid - 1; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} else { | ||
if (nums[mid] < target && target <= nums[right]) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} |
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.
Greey, DP, DFS 등 다양한 방식으로 시간복잡도와 공간복잡도를 고려해서 진행한 풀이 방식이 인상 깊었습니다.