-
-
Notifications
You must be signed in to change notification settings - Fork 195
[gitsunmin] WEEK 2 Solution #362
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
7 commits
Select commit
Hold shift + click to select a range
c6d37af
Add week 2 solutions: valid-anagram
gitsunmin 71307c3
Add week 2 solutions: counting-bits
gitsunmin 24e7e83
Add week 2 solutions: encode-and-decode-strings
gitsunmin 56eafe1
Add week 2 solutions: construct-binary-tree-from-preorder-and-inorder…
gitsunmin b441798
Merge branch 'DaleStudy:main' into main
gitsunmin ad8c107
🚨 add line break
gitsunmin d2f8a23
Merge branch 'DaleStudy:main' into main
gitsunmin 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
60 changes: 60 additions & 0 deletions
60
construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts
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,60 @@ | ||
|
||
/** | ||
* * 문제에서 정의된 타입 | ||
*/ | ||
export 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) | ||
} | ||
} | ||
|
||
/** | ||
* ! 문제에서의 Output과 실제 정의된, 사용되는 output이 다르기 때문에, 한 번 변환 작업을 거처야함. (실제 제출 시 제외한 함수 입니다.) | ||
*/ | ||
// function treeToArray(root: TreeNode | null): (number | null)[] { | ||
// if (!root) return []; | ||
// const result: (number | null)[] = []; | ||
// const queue: (TreeNode | null)[] = [root]; | ||
// while (queue.length > 0) { | ||
// const node = queue.shift(); | ||
// if (node) { | ||
// result.push(node.val); | ||
// queue.push(node.left); | ||
// queue.push(node.right); | ||
// } else { | ||
// result.push(null); | ||
// } | ||
// } | ||
// while (result[result.length - 1] === null) result.pop(); | ||
// return result; | ||
// } | ||
|
||
function buildTree(preorder: number[], inorder: number[]): TreeNode | null { | ||
if (preorder.length === 0 || inorder.length === 0) return null; | ||
|
||
const rootVal = preorder[0]; | ||
const inorderIndex = inorder.indexOf(rootVal); | ||
const leftInorder = inorder.slice(0, inorderIndex); | ||
|
||
return new TreeNode( | ||
rootVal, | ||
buildTree( | ||
preorder.slice(1, 1 + leftInorder.length), | ||
leftInorder | ||
), | ||
buildTree( | ||
preorder.slice(1 + leftInorder.length), | ||
inorder.slice(inorderIndex + 1) | ||
), | ||
); | ||
} | ||
|
||
|
||
// const preorder = [3, 9, 20, 15, 7]; | ||
// const inorder = [9, 3, 15, 20, 7]; | ||
// console.log('output:', treeToArray(buildTree(preorder, inorder))); |
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,10 @@ | ||
/** | ||
* https://leetcode.com/problems/counting-bits/ | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
function countBits(n: number): number[] { | ||
const arr = new Array(n + 1).fill(0); | ||
for (let i = 1; i <= n; i++) arr[i] = arr[i >> 1] + (i & 1); | ||
return arr; | ||
} |
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,14 @@ | ||
/** | ||
* https://www.lintcode.com/problem/659/ | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
* ! emoji는 입력에서 제외한다. | ||
*/ | ||
|
||
function encode(input: Array<string>): string { | ||
return input.join("😀"); | ||
} | ||
|
||
function decode(input: string): Array<string> { | ||
return input.split("😀"); | ||
} |
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,22 @@ | ||
/** | ||
* https://leetcode.com/problems/valid-anagram/submissions | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
function isAnagram(s: string, t: string): boolean { | ||
if (s.length !== t.length) return false; | ||
|
||
const map = {}; | ||
|
||
for (const char of s) map[char] = (map[char] ?? 0) + 1; | ||
|
||
for (const char of t) { | ||
if (map[char] !== undefined) { | ||
map[char] = map[char] - 1; | ||
} else return false; | ||
} | ||
|
||
for (const val of Object.values(map)) if (val !== 0) return false; | ||
|
||
return true; | ||
}; |
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.
문제에 다른 제약이 없어서 특수문자 대신에 이모지를 넣어봤어요 ㅎ