-
-
Notifications
You must be signed in to change notification settings - Fork 195
[hsskey] Week 10 Solutions #1558
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
+165
−0
Merged
Changes from all commits
Commits
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,37 @@ | ||
/** | ||
* @param {number} numCourses | ||
* @param {number[][]} prerequisites | ||
* @return {boolean} | ||
*/ | ||
var canFinish = function(numCourses, prerequisites) { | ||
const preMap = {}; | ||
for (let i = 0; i < numCourses; i++) { | ||
preMap[i] = []; | ||
} | ||
for (const [crs, pre] of prerequisites) { | ||
preMap[crs].push(pre); | ||
} | ||
|
||
const visitSet = new Set(); | ||
|
||
const dfs = (crs) => { | ||
if (visitSet.has(crs)) return false; | ||
if (preMap[crs].length === 0) return true; | ||
|
||
visitSet.add(crs); | ||
for (const pre of preMap[crs]) { | ||
if (!dfs(pre)) return false; | ||
} | ||
visitSet.delete(crs); | ||
preMap[crs] = []; | ||
|
||
return true; | ||
}; | ||
|
||
for (let crs = 0; crs < numCourses; crs++) { | ||
if (!dfs(crs)) 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,27 @@ | ||
/** | ||
* 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 {TreeNode} | ||
*/ | ||
var invertTree = function(root) { | ||
if (!root) return null; | ||
|
||
// swap the children | ||
let tmp = root.left; | ||
root.left = root.right; | ||
root.right = tmp; | ||
|
||
invertTree(root.left); | ||
invertTree(root.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,16 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var canJump = function(nums) { | ||
let goal = nums.length - 1; | ||
|
||
for (let i = nums.length - 1; i >= 0; i--) { | ||
if (i + nums[i] >= goal) { | ||
goal = i; | ||
} | ||
} | ||
|
||
return goal === 0; | ||
}; | ||
|
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,49 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {ListNode[]} lists | ||
* @return {ListNode} | ||
*/ | ||
var mergeKLists = function(lists) { | ||
if (!lists || lists.length === 0) return null; | ||
|
||
const mergeList = (l1, l2) => { | ||
const dummy = new ListNode(0); | ||
let current = dummy; | ||
|
||
while (l1 && l2) { | ||
if (l1.val < l2.val) { | ||
current.next = l1; | ||
l1 = l1.next; | ||
} else { | ||
current.next = l2; | ||
l2 = l2.next; | ||
} | ||
current = current.next; | ||
} | ||
|
||
current.next = l1 || l2; | ||
return dummy.next; | ||
}; | ||
|
||
while (lists.length > 1) { | ||
const mergedLists = []; | ||
|
||
for (let i = 0; i < lists.length; i += 2) { | ||
const l1 = lists[i]; | ||
const l2 = (i + 1 < lists.length) ? lists[i + 1] : null; | ||
mergedLists.push(mergeList(l1, l2)); | ||
} | ||
|
||
lists = mergedLists; | ||
} | ||
|
||
return lists[0]; | ||
}; | ||
|
||
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,36 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number} | ||
*/ | ||
var search = function(nums, target) { | ||
let l = 0, r = nums.length - 1; | ||
|
||
while (l <= r) { | ||
const mid = Math.floor((l + r) / 2); | ||
|
||
if (nums[mid] === target) { | ||
return mid; | ||
} | ||
|
||
// left sorted portion | ||
if (nums[l] <= nums[mid]) { | ||
if (target > nums[mid] || target < nums[l]) { | ||
l = mid + 1; | ||
} else { | ||
r = mid - 1; | ||
} | ||
} | ||
// right sorted portion | ||
else { | ||
if (target < nums[mid] || target > nums[r]) { | ||
r = mid - 1; | ||
} else { | ||
l = 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.
오! 이런 방식으로 merge를 해서 풀 수가 있는 것을 알았네요
자바라서 내장 콜렉션 함수로 풀었는데 모든 언어에 대해 풀 수 있을려면 이 방식이 더 좋은 것 같네요