-
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
[Tony] WEEK 11 Solutions #547
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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 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,24 @@ | ||
// TC: O(n) | ||
// visit all nodes to find maximum path | ||
// SC: O(h) | ||
// h means the high of binary tree | ||
class Solution { | ||
private int output = Integer.MIN_VALUE; | ||
public int maxPathSum(TreeNode root) { | ||
max(root); | ||
return output; | ||
} | ||
|
||
private int max(TreeNode node) { | ||
if (node == null) return 0; | ||
|
||
int leftSum = Math.max(max(node.left), 0); | ||
int rightSum = Math.max(max(node.right), 0); | ||
|
||
int currentMax = node.val + leftSum + rightSum; | ||
|
||
output = Math.max(output, currentMax); | ||
|
||
return node.val + Math.max(leftSum, rightSum); | ||
} | ||
} |
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,37 @@ | ||
// TC: O(n * m) | ||
// n = the length of edges, m = the length of each element list of edges | ||
// SC: O(n) | ||
// n = recursively visit everywhere | ||
public class Solution { | ||
public boolean validTree(int n, int[][] edges) { | ||
if (edges.length != n - 1) return false; | ||
|
||
List<List<Integer>> graph = new ArrayList<>(); | ||
for (int i = 0; i < n; i++) { | ||
graph.add(new ArrayList<>()); | ||
} | ||
|
||
for (int[] edge : edges) { | ||
graph.get(edge[0]).add(edge[1]); | ||
graph.get(edge[1]).add(edge[0]); | ||
} | ||
|
||
boolean[] visited = new boolean[n]; | ||
if (!dfs(0, -1, graph, visited)) return false; | ||
|
||
for (boolean v : visited) if(!v) return false; | ||
|
||
return true; | ||
} | ||
|
||
private boolean dfs(int node, int parent, List<List<Integer>> graph, boolean[] visited) { | ||
visited[node] = true; | ||
|
||
for (int neighbor : graph.get(node)) { | ||
if (neighbor == parent) continue; | ||
if (visited[neighbor]) return false; | ||
if (!dfs(neighbor, node, graph, visited)) return false; | ||
} | ||
return true; | ||
} | ||
} |
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,32 @@ | ||
// TC: O(n) | ||
// must retrieve all elements | ||
// SC: O(n) | ||
// need the same size of memory space in the worst case | ||
class Solution { | ||
public int[][] insert(int[][] intervals, int[] newInterval) { | ||
List<int[]> output = new ArrayList<>(); | ||
|
||
int i = 0; | ||
int n = intervals.length; | ||
|
||
while (i < n && intervals[i][1] < newInterval[0]) { | ||
output.add(intervals[i]); | ||
i += 1; | ||
} | ||
|
||
while (i < n && intervals[i][0] <= newInterval[1]) { | ||
newInterval[0] = Math.min(newInterval[0], intervals[i][0]); | ||
newInterval[1] = Math.max(newInterval[1], intervals[i][1]); | ||
i += 1; | ||
} | ||
|
||
output.add(newInterval); | ||
|
||
while (i < n) { | ||
output.add(intervals[i]); | ||
i += 1; | ||
} | ||
|
||
return output.toArray(new int[output.size()][]); | ||
} | ||
} |
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,15 @@ | ||
// TC: O(n) | ||
// visiting all nodes | ||
// SC: O(1) | ||
// constant space complexity | ||
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. 엇 재귀 스택도 고려해주셔야 할 것 같습니다! |
||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
return getMax(root, 0); | ||
} | ||
|
||
private int getMax(TreeNode node, int depth) { | ||
if (node == null) return depth; | ||
depth += 1; | ||
return Math.max(getMax(node.left, depth), getMax(node.right, depth)); | ||
} | ||
} |
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,34 @@ | ||
// TC: O(n) | ||
// -> find middle, reverse, merge -> max O(n) | ||
// SC: O(1) | ||
class Solution { | ||
public void reorderList(ListNode head) { | ||
if (head == null || head.next == null) return; | ||
|
||
ListNode slow = head, fast = head; | ||
while (fast != null && fast.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
ListNode backSide = null; | ||
ListNode curr = slow.next; | ||
slow.next = null; | ||
|
||
while (curr != null) { | ||
ListNode temp = curr.next; | ||
curr.next = backSide; | ||
backSide = curr; | ||
curr = temp; | ||
} | ||
|
||
ListNode first = head; | ||
ListNode second = backSide; | ||
while (second != null) { | ||
ListNode temp = first.next; | ||
first.next = second; | ||
first = second; | ||
second = temp; | ||
} | ||
} | ||
} |
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.
시간 복잡도 판단에 대해 혹시 설명해주실 수 있을까요?
주석에서는(제가 이해하기로는) m이 각 노드에 연결된 엣지 수의 평균이라고 작성해 주신 것 같은데,
그렇다면 현재 문제에서 최대 엣지의 수가 n-1이라는 점을 고려하면 m은 결국 1(상수)가 되지 않을까요?