-
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
[bky373] Week 3 Solutions #75
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da53272
[week3] solve 104. Maximum Depth of Binary Tree
bky373 f636cb1
[week3] solve 100. Same Tree
bky373 f872aad
[week3] solve 572. Subtree of Another Tree
bky373 f7093c5
[week3] solve 70. Climbing Stairs
bky373 d2358f7
[week3] solve 252. Meeting Rooms
bky373 9e8fd5f
[week3] improvement for 70. Climbing Stairs
bky373 9b5e3ab
[week3] fix space complexity for 572. Subtree of Another Tree
bky373 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,20 @@ | ||
class Solution { | ||
|
||
public int climbStairs(int n) { | ||
int[] ans = new int[n + 1]; | ||
if (n <= 2) { | ||
return n; | ||
} | ||
ans[1] = 1; | ||
ans[2] = 2; | ||
|
||
for (int i = 3; i < n + 1; i++) { | ||
ans[i] = ans[i - 1] + ans[i - 2]; | ||
} | ||
return ans[n]; | ||
} | ||
} | ||
/** | ||
* TC: O(N) | ||
* SC: O(N) | ||
*/ |
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,17 @@ | ||
/** | ||
* TC: O(N) | ||
* SC: O(N) | ||
*/ | ||
class Solution { | ||
|
||
public int maxDepth(TreeNode root) { | ||
return findMax(root, 0); | ||
} | ||
|
||
public int findMax(TreeNode node, int depth) { | ||
if (node == null) { | ||
return depth; | ||
} | ||
return Math.max(findMax(node.left, depth + 1), findMax(node.right, depth + 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,19 @@ | ||
class Solution { | ||
|
||
public boolean canAttendMeetings(int[][] intervals) { | ||
if (intervals.length == 0) { | ||
return true; | ||
} | ||
Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]); | ||
for (int i = 0; i < intervals.length - 1; i++) { | ||
if (intervals[i][1] > intervals[i + 1][0]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
/** | ||
* TC: O(nlogn), due to the sorting array. | ||
* SC: O(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,11 @@ | ||
/** | ||
* TC: O(N) | ||
* SC: O(N) | ||
*/ | ||
class Solution { | ||
public boolean isSameTree(TreeNode p, TreeNode q) { | ||
if (p == null && q == null) return true; | ||
if (p == null || q == null || p.val != q.val) return false; | ||
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
} | ||
} |
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(M * N) - M: 메인 트리(root)의 노드 수, N: 서브 트리(subtree)의 노드 수 | ||
* SC: O(N) - N: 메인 트리(root)의 높이 | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
class Solution { | ||
public boolean isSubtree(TreeNode root, TreeNode subRoot) { | ||
if (root == null) { | ||
return false; | ||
} | ||
if (isSameTree(root, subRoot)) { | ||
return true; | ||
} | ||
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); | ||
} | ||
|
||
public boolean isSameTree(TreeNode n1, TreeNode n2) { | ||
if (n1 == null || n2 == null) { | ||
return n1 == n2; | ||
} | ||
if (n1.val != n2.val) { | ||
return false; | ||
} | ||
return isSameTree(n1.left, n2.left) && isSameTree(n1.right, n2.right); | ||
} | ||
} |
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.
좋은 피드백이네요 😃
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.
와.. 이런 접근이 가능했군요! 문제 새로 풀어두었습니다 감사합니다!
[week3] improvement for 70. Climbing Stairs
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.
져도 처음에는 list에 쌓았는데 피드백 받아서 고쳤습니다ㅎㅎ