-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jdy8739] Week 15 #1108
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
[jdy8739] Week 15 #1108
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,33 @@ | ||
/** | ||
* @param {number[][]} matrix | ||
* @return {void} Do not return anything, modify matrix in-place instead. | ||
*/ | ||
var rotate = function (matrix) { | ||
const rotateMatrix = (matrix, start) => { | ||
const length = matrix.length; | ||
|
||
const end = length - 1 - start; | ||
|
||
if (end <= start) { | ||
return; | ||
} | ||
|
||
for (let i = start; i < end; i++) { | ||
const temp = matrix[start][i]; | ||
|
||
const target = length - 1 - i; | ||
|
||
matrix[start][i] = matrix[target][start]; | ||
matrix[target][start] = matrix[end][target]; | ||
matrix[end][target] = matrix[i][end]; | ||
matrix[i][end] = temp; | ||
} | ||
|
||
rotateMatrix(matrix, start + 1); | ||
} | ||
|
||
rotateMatrix(matrix, 0); | ||
}; | ||
|
||
// 시간복잡도 O(n^2) | ||
// 공간복잡도 O(l) -> 재귀호출 스택이 최대 매트릭스의 길이 / 2로 최대 쌓이므로 |
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,40 @@ | ||
/** | ||
* 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 | ||
* @param {TreeNode} subRoot | ||
* @return {boolean} | ||
*/ | ||
var isSubtree = function (root, subRoot) { | ||
const tree = [`'${root?.val}'`]; | ||
const subTree = [`'${subRoot?.val}'`]; | ||
|
||
const dfsStringify = (node, arr) => { | ||
arr.push(`'${node.left?.val}'` ?? 'null'); | ||
if (node?.left) { | ||
dfsStringify(node.left, arr); | ||
} | ||
|
||
arr.push(`'${node.right?.val}'` ?? 'null'); | ||
if (node?.right) { | ||
dfsStringify(node.right, arr); | ||
} | ||
} | ||
|
||
dfsStringify(root, tree); | ||
dfsStringify(subRoot, subTree); | ||
|
||
const treeString = tree.join(','); | ||
const subTreeString = subTree.join(','); | ||
|
||
return treeString.includes(subTreeString); | ||
}; | ||
|
||
// 시간복잡도 O(n + m) -> root tree와 subRoot tree의 노드를 모두 방문하기때문에 root의 노드 수인 n과 subRoot의 노드 수인 m을 더한 값 | ||
// 공간복잡도 O(n + m) -> 두 트리의 노드 수를 저장하는 배열의 크기가 각각 n과 m이기 때문 |
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. 범위를 표현하는 방식만 조금 다르고, 저도 같은 방법으로 문제를 풀었습니다! |
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,30 @@ | ||
/** | ||
* 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 {boolean} | ||
*/ | ||
var isValidBST = function (root) { | ||
const dfs = (node, min, max) => { | ||
if (!node) { | ||
return true; | ||
} | ||
|
||
if ((min !== undefined && node.val <= min) || (max !== undefined && node.val >= max)) { | ||
return false; | ||
} | ||
|
||
return dfs(node.left, min, node.val) && dfs(node.right, node.val, max); | ||
}; | ||
|
||
return dfs(root, undefined, undefined); | ||
}; | ||
|
||
// 시간복잡도 O(n) -> 최악의 경우인 완전한 이진 트리의 경우 모든 노드를 방문해서 확인해야 함 | ||
// 공간복잡도 o(h) -> 최대 트리의 높이만큼 함수호출 스택에 함수컨텍스트가 쌓임 |
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.
Uh oh!
There was an error while loading. Please reload this page.