-
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 2 Solution #360
Merged
+63
−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
14 changes: 14 additions & 0 deletions
14
construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.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,14 @@ | ||
// 시간복잡도: O(n2) | ||
// 공간복잡도: O(n) | ||
|
||
var buildTree = function(preorder, inorder) { | ||
if (!preorder.length || !inorder.length) return null; | ||
|
||
const root = new TreeNode(preorder[0]); | ||
const mid = inorder.indexOf(root.val); | ||
|
||
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid)); | ||
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1)); | ||
|
||
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,21 @@ | ||
// 시간복잡도 O(n * log n) | ||
// 공간복잡도 O(n) | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number[]} | ||
*/ | ||
var countBits = function(n) { | ||
const result = [] | ||
|
||
for (let i = 0 ; i <= n; i++) { | ||
const binaryNumber = i.toString(2) | ||
const oneLength = binaryNumber.replaceAll('0','').length | ||
|
||
result.push(oneLength) | ||
} | ||
|
||
return result | ||
}; | ||
|
||
console.log(countBits(5)) |
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,28 @@ | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(k) | ||
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.
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. 저장되는 문자열 개수를 뜻했는데 어처피 문자는 정해져있으니 O(1)이 될 수도 있겠네요..! |
||
|
||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function(s, t) { | ||
if (s.length !== t.length) return false | ||
|
||
const checkMap = new Map(); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
checkMap.set(s[i], (checkMap.get(s[i]) || 0) + 1) | ||
} | ||
|
||
|
||
for (let j = 0; j < t.length; j++) { | ||
if (!checkMap.get(t[j]) || checkMap.get(t[j]) < 0) return false | ||
checkMap.set(t[j], (checkMap.get(t[j]) || 0 ) - 1); | ||
} | ||
|
||
return true | ||
}; | ||
|
||
|
||
console.log(isAnagram("anagram","nagaram")) |
Oops, something went wrong.
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.
이 부분은 헷갈려서 다른 분 시간복잡도를 참고했습니다.