-
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 02 Solutions #339
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c5c3cf
solve: valid anagram
wogha95 6d43c39
fix: line lint
wogha95 3855d02
solve: counting bits
wogha95 a0ef72e
solve: construct binary tree from preorder and inorder traversal
wogha95 4f38c55
solve: decode ways
wogha95 ff09bff
review: construct binary tree from preorder and inorder traversal
wogha95 835af9b
review: construct binary tree from preorder and inorder traversal
wogha95 6388615
review: construct binary tree from preorder and inorder traversal
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
33 changes: 33 additions & 0 deletions
33
construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js
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,33 @@ | ||
// TC: O(N^2) | ||
// SC: O(N^2) | ||
|
||
/** | ||
* 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 {number[]} preorder | ||
* @param {number[]} inorder | ||
* @return {TreeNode} | ||
*/ | ||
var buildTree = function (preorder, inorder) { | ||
if (inorder.length === 0) { | ||
return null; | ||
} | ||
|
||
const rootValue = preorder[0]; | ||
const leftNodeLength = inorder.findIndex((value) => value === rootValue); | ||
const leftNode = buildTree( | ||
preorder.slice(1, 1 + leftNodeLength), | ||
inorder.slice(0, leftNodeLength) | ||
); | ||
const rightNode = buildTree( | ||
preorder.slice(1 + leftNodeLength), | ||
inorder.slice(leftNodeLength + 1) | ||
); | ||
return new TreeNode(rootValue, leftNode, rightNode); | ||
}; |
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,25 @@ | ||
// TC: O(N) | ||
// SC: O(N) | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number[]} | ||
*/ | ||
var countBits = function (n) { | ||
const result = [0]; | ||
let pointer = 0; | ||
let lastPointer = 0; | ||
|
||
for (let num = 1; num <= n; num++) { | ||
result.push(result[pointer] + 1); | ||
|
||
if (pointer === lastPointer) { | ||
lastPointer = result.length - 1; | ||
pointer = 0; | ||
} else { | ||
pointer += 1; | ||
} | ||
} | ||
|
||
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,48 @@ | ||
// TC: O(N) | ||
// SC: O(N) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var numDecodings = function (s) { | ||
if (s[0] === "0") { | ||
return 0; | ||
} | ||
if (s.length === 1) { | ||
return 1; | ||
} | ||
|
||
const dpTable = new Array(s.length).fill(0); | ||
if (s[0] !== "0") { | ||
dpTable[0] = 1; | ||
} | ||
if (s[1] !== "0") { | ||
dpTable[1] += 1; | ||
} | ||
if (isValid(`${s[0]}${s[1]}`)) { | ||
dpTable[1] += 1; | ||
} | ||
|
||
for (let index = 2; index < s.length; index++) { | ||
if (s[index] !== "0") { | ||
dpTable[index] += dpTable[index - 1]; | ||
} | ||
if (s[index - 1] !== "0" && isValid(`${s[index - 1]}${s[index]}`)) { | ||
dpTable[index] += dpTable[index - 2]; | ||
} | ||
} | ||
|
||
return dpTable[dpTable.length - 1]; | ||
|
||
function isValid(stringNumber) { | ||
const number = Number(stringNumber); | ||
if (number <= 0) { | ||
return false; | ||
} | ||
if (27 <= number) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; |
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,11 @@ | ||
// TC: O(N * log N) | ||
// SC: O(N) | ||
|
||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function(s, t) { | ||
return s.split('').sort().join('') === t.split('').sort().join(''); | ||
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. 오..... 나누고 정렬하고 합치고 좋네요 배워갑니다! |
||
}; |
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.
이 투포인터 솔루션 신기한 것 같은데, 혹시 모임 때 소개해주실 수 있으실까요?
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.
달레님 알고리즘과 크게 다를 바 없다고 생각해서 조금 의아했는데 소개 이유를 들어보니 깨닫게 되었습니다!
그리고 갑작스럽게 발표준비하다보니 두서없이 설명드린것 같아서 걱정되었는데 부족한 부분 피드백 주셔서 감사합니다 :)