-
Notifications
You must be signed in to change notification settings - Fork 126
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
[재호] WEEK 14 Solutions #596
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5dcc0f7
solve: house robber
wogha95 e354f1d
solve: lowest common ancestor of a binary search tree
wogha95 0c2d28b
solve: non overlapping intervals
wogha95 22f172e
solve: reverse bits
wogha95 a43dd47
solve: binary tree level order traversal
wogha95 ecc65dd
solve: binary tree level order traversal
wogha95 087e190
solve: house robber ii
wogha95 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 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,114 @@ | ||
/** | ||
* 2차 | ||
* 각 level의 node 수만큼 끊어서 순회하기 | ||
* | ||
* TC: O(N) | ||
* SC: O(N) | ||
* N: total of nodes | ||
*/ | ||
|
||
/** | ||
* 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 levelOrder = function (root) { | ||
if (!root) { | ||
return []; | ||
} | ||
|
||
const result = []; | ||
const queue = [root]; | ||
|
||
while (queue.length > 0) { | ||
const level = queue.length; | ||
const currentLevelValList = []; | ||
|
||
for (let i = 0; i < level; i++) { | ||
const current = queue.shift(); | ||
|
||
currentLevelValList.push(current.val); | ||
|
||
if (current.left) { | ||
queue.push(current.left); | ||
} | ||
|
||
if (current.right) { | ||
queue.push(current.right); | ||
} | ||
} | ||
|
||
result.push(currentLevelValList); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
/** | ||
* 1차 | ||
* level과 노드를 queue에 추가해서 정답만들기 | ||
* | ||
* TC: O(N) | ||
* SC: O(N) | ||
* N: total of nodes | ||
*/ | ||
|
||
/** | ||
* 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 levelOrder = function (root) { | ||
const result = []; | ||
|
||
const queue = [ | ||
{ | ||
level: 0, | ||
current: root, | ||
}, | ||
]; | ||
|
||
while (queue.length > 0) { | ||
const { level, current } = queue.shift(); | ||
|
||
if (!current) { | ||
continue; | ||
} | ||
|
||
if (result[level]) { | ||
result[level].push(current.val); | ||
} else { | ||
result[level] = [current.val]; | ||
} | ||
|
||
if (current.left) { | ||
queue.push({ | ||
level: level + 1, | ||
current: current.left, | ||
}); | ||
} | ||
|
||
if (current.right) { | ||
queue.push({ | ||
level: level + 1, | ||
current: current.right, | ||
}); | ||
} | ||
} | ||
|
||
return result; | ||
}; |
This file contains 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,44 @@ | ||
/** | ||
* TC: O(N) | ||
* SC; O(1) | ||
*/ | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function (nums) { | ||
if (nums.length < 4) { | ||
return Math.max(...nums); | ||
} | ||
|
||
let prevprevprev = nums[0]; | ||
let prevprev = nums[1]; | ||
let prev = nums[0] + nums[2]; | ||
|
||
for (let index = 3; index < nums.length - 1; index++) { | ||
const current = Math.max(prevprevprev, prevprev) + nums[index]; | ||
|
||
prevprevprev = prevprev; | ||
prevprev = prev; | ||
prev = current; | ||
} | ||
|
||
const resultWithoutLast = Math.max(prevprevprev, prevprev, prev); | ||
|
||
prevprevprev = nums[1]; | ||
prevprev = nums[2]; | ||
prev = nums[1] + nums[3]; | ||
|
||
for (let index = 4; index < nums.length; index++) { | ||
const current = Math.max(prevprevprev, prevprev) + nums[index]; | ||
|
||
prevprevprev = prevprev; | ||
prevprev = prev; | ||
prev = current; | ||
} | ||
|
||
const resultWithoutFirst = Math.max(prevprevprev, prevprev, prev); | ||
|
||
return Math.max(resultWithoutLast, resultWithoutFirst); | ||
}; |
This file contains 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,31 @@ | ||
/** | ||
* dp[n] = n위치의 집을 훔친다는 가정하에 n위치의 집까지 최대로 훔친 금액 | ||
* dp[n] = Math.max(dp[n - 3], dp[n - 2]) + nums[index] | ||
* | ||
* TC: O(N) | ||
* SC: O(1) | ||
*/ | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function (nums) { | ||
if (nums.length < 3) { | ||
return Math.max(...nums); | ||
} | ||
|
||
let prevprevprev = nums[0]; | ||
let prevprev = nums[1]; | ||
let prev = nums[0] + nums[2]; | ||
|
||
for (let index = 3; index < nums.length; index++) { | ||
const current = Math.max(prevprevprev, prevprev) + nums[index]; | ||
|
||
prevprevprev = prevprev; | ||
prevprev = prev; | ||
prev = current; | ||
} | ||
|
||
return Math.max(prevprevprev, prevprev, prev); | ||
}; |
This file contains 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 @@ | ||
/** | ||
* TC: O(logN) | ||
* left와 right 중 한 곳으로만 탐색 | ||
* | ||
* SC: O(1) | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {TreeNode} root | ||
* @param {TreeNode} p | ||
* @param {TreeNode} q | ||
* @return {TreeNode} | ||
*/ | ||
var lowestCommonAncestor = function (root, p, q) { | ||
while (root) { | ||
if (root.val > p.val && root.val > q.val) { | ||
root = root.left; | ||
} else if (root.val < p.val && root.val < q.val) { | ||
root = root.right; | ||
} else { | ||
return root; | ||
} | ||
} | ||
}; |
This file contains 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 @@ | ||
/** | ||
* TC: O(N * logN) | ||
* 정렬로 인한 시간복잡도 | ||
* | ||
* SC: O(1) | ||
*/ | ||
|
||
/** | ||
* @param {number[][]} intervals | ||
* @return {number} | ||
*/ | ||
var eraseOverlapIntervals = function (intervals) { | ||
intervals.sort((a, b) => a[1] - b[1]); | ||
|
||
let count = 0; | ||
let lastEnd = Number.MIN_SAFE_INTEGER; | ||
|
||
for (const [start, end] of intervals) { | ||
if (start < lastEnd) { | ||
count += 1; | ||
} else { | ||
lastEnd = end; | ||
} | ||
} | ||
|
||
return count; | ||
}; |
This file contains 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,15 @@ | ||
/** | ||
* TC: O(1) | ||
* n 숫자를 32자리 2진수로 변환하고 배열로 변경했을때 최대 길이가 32이므로 상수 복잡도를 갖는다. | ||
* | ||
* SC: O(1) | ||
*/ | ||
|
||
/** | ||
* @param {number} n - a positive integer | ||
* @return {number} - a positive integer | ||
*/ | ||
var reverseBits = function (n) { | ||
const result = n.toString(2).padStart(32, "0").split("").reverse().join(""); | ||
return parseInt(result, 2); | ||
}; |
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.
메모리를 아낄 수 있는 좋은 접근방법인 것 같습니다. 다만 디버깅은 조금 어려운 단점이 있을 것 같습니다.