-
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
[anniemon78] Week 2 #748
Merged
Merged
[anniemon78] Week 2 #748
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
04e7e83
feat: valid anagram
anniemon 2417976
feat: climbing stairs
anniemon 3e03de7
feat: 3sum
anniemon fb0dab1
feat: construct binary tree from preorder and inorder traversal
anniemon cbebb81
modify: memo from map to list
anniemon 3274855
modify: insert empty line for readability
anniemon 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,40 @@ | ||
/** | ||
* 시간 복잡도: | ||
* nums의 길이만큼 for문을 순회하고, 내부에서 투 포인터로 또 한 번 순회하므로, O(n²) | ||
* 공간 복잡도: | ||
* 정렬은 추가 공간 사용이 없음. | ||
* res 배열의 크기는 고유한 세 숫자 조합의 갯수. | ||
* 이를 k라고 하면, 공간 복잡도는 O(k) | ||
*/ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function (nums) { | ||
nums.sort((a, b) => a - b); | ||
const res = []; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
if (i > 0 && nums[i] === nums[i - 1]) { | ||
continue; | ||
} | ||
let l = i + 1; | ||
let r = nums.length - 1; | ||
while (l < r) { | ||
const sum = nums[i] + nums[l] + nums[r]; | ||
if (sum > 0) { | ||
r--; | ||
} else if (sum < 0) { | ||
l++; | ||
} else if (sum === 0) { | ||
res.push([nums[i], nums[l], nums[r]]); | ||
l++; | ||
r--; | ||
while (l < r && nums[l] === nums[l - 1]) { | ||
l++; | ||
} | ||
} | ||
} | ||
} | ||
return res; | ||
}; |
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 @@ | ||
/** | ||
* 시간 복잡도: | ||
* 메모이제이션을 사용하여 n까지 가기 위한 방법의 수를 저장. | ||
* 재귀 함수는 최대 n만큼 호출됨. | ||
* 따라서, 시간 복잡도는 O(n) | ||
* 공간 복잡도: | ||
* 메모 객체의 크기는 n의 크기와 같음. | ||
* 재귀 함수는 최대 n만큼 호출됨. | ||
* 따라서, 공간 복잡도는 O(n) | ||
*/ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function(n) { | ||
const memo = { 1:1, 2:2 }; | ||
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. 만약 제가 면접관이라면 왜 list가 아닌 map을 사용했는지 물어볼 것 같습니다. 개인적으로 이전 단계의 값으로 다음 단계의 값을 구한다는 의미에는 list가 더 적합하지 않을까 싶습니다. 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. 좋은 포인트네요. 반영했습니다. |
||
const recurse = (n) => { | ||
if(memo[n]) { | ||
return memo[n]; | ||
} | ||
memo[n] = recurse(n - 1) + recurse(n - 2); | ||
return memo[n]; | ||
} | ||
return recurse(n); | ||
}; |
40 changes: 40 additions & 0 deletions
40
construct-binary-tree-from-preorder-and-inorder-traversal/anniemon.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,40 @@ | ||
/** | ||
* 시간 복잡도: | ||
* preIdx를 이용하여 중위 순회 배열에서 루트 노드를 기준으로 왼쪽 서브 트리와 오른쪽 서브 트리를 탐색. | ||
* 각 서브 트리를 재귀적으로 생성하며 모든 노드를 한 번씩 순회하므로, 시간 복잡도는 O(n) | ||
* 공간 복잡도: | ||
* 중위 순회 배열의 길이만큼 맵을 생성하므로, 공간 복잡도는 O(n) | ||
*/ | ||
/** | ||
* 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) { | ||
let preIdx = 0; | ||
const inorderMap = new Map(inorder.map((e, i) => [e, i])) | ||
|
||
const dfs = (l, r) => { | ||
if(l > r) { | ||
return null; | ||
} | ||
let root = preorder[preIdx]; | ||
preIdx++; | ||
|
||
let rootIdx = inorderMap.get(root); | ||
|
||
const node = new TreeNode(root); | ||
node.left = dfs(l, rootIdx - 1); | ||
node.right = dfs(rootIdx + 1, r); | ||
return node; | ||
} | ||
return dfs(0, inorder.length - 1) | ||
}; |
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,29 @@ | ||
/** | ||
* 시간 복잡도: | ||
* s와 t의 길이만큼 각 문자의 카운트를 기록하고 이를 확인하므로, 시간 복잡도는 O(n) | ||
* 공간 복잡도: | ||
* 카운트 객체는 최대 s와 t의 길이만큼 공간을 차지하므로, 공간 복잡도는 O(n) | ||
*/ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function (s, t) { | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
|
||
const count = {}; | ||
for (let i = 0; i < s.length; i++) { | ||
count[s[i]] = (count[s[i]] || 0) + 1; | ||
count[t[i]] = (count[t[i]] || 0) - 1; | ||
} | ||
|
||
for (const key in count) { | ||
if (count[key] !== 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; |
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.
넵 반영했습니다