Skip to content

Commit af10a3a

Browse files
authored
Merge pull request #547 from TonyKim9401/main
[Tony] WEEK 11 Solutions
2 parents 92d3690 + c243ab3 commit af10a3a

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// TC: O(n)
2+
// visit all nodes to find maximum path
3+
// SC: O(h)
4+
// h means the high of binary tree
5+
class Solution {
6+
private int output = Integer.MIN_VALUE;
7+
public int maxPathSum(TreeNode root) {
8+
max(root);
9+
return output;
10+
}
11+
12+
private int max(TreeNode node) {
13+
if (node == null) return 0;
14+
15+
int leftSum = Math.max(max(node.left), 0);
16+
int rightSum = Math.max(max(node.right), 0);
17+
18+
int currentMax = node.val + leftSum + rightSum;
19+
20+
output = Math.max(output, currentMax);
21+
22+
return node.val + Math.max(leftSum, rightSum);
23+
}
24+
}

graph-valid-tree/TonyKim9401.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// TC: O(n * m)
2+
// n = the length of edges, m = the length of each element list of edges
3+
// SC: O(n)
4+
// n = recursively visit everywhere
5+
public class Solution {
6+
public boolean validTree(int n, int[][] edges) {
7+
if (edges.length != n - 1) return false;
8+
9+
List<List<Integer>> graph = new ArrayList<>();
10+
for (int i = 0; i < n; i++) {
11+
graph.add(new ArrayList<>());
12+
}
13+
14+
for (int[] edge : edges) {
15+
graph.get(edge[0]).add(edge[1]);
16+
graph.get(edge[1]).add(edge[0]);
17+
}
18+
19+
boolean[] visited = new boolean[n];
20+
if (!dfs(0, -1, graph, visited)) return false;
21+
22+
for (boolean v : visited) if(!v) return false;
23+
24+
return true;
25+
}
26+
27+
private boolean dfs(int node, int parent, List<List<Integer>> graph, boolean[] visited) {
28+
visited[node] = true;
29+
30+
for (int neighbor : graph.get(node)) {
31+
if (neighbor == parent) continue;
32+
if (visited[neighbor]) return false;
33+
if (!dfs(neighbor, node, graph, visited)) return false;
34+
}
35+
return true;
36+
}
37+
}

insert-interval/TonyKim9401.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// TC: O(n)
2+
// must retrieve all elements
3+
// SC: O(n)
4+
// need the same size of memory space in the worst case
5+
class Solution {
6+
public int[][] insert(int[][] intervals, int[] newInterval) {
7+
List<int[]> output = new ArrayList<>();
8+
9+
int i = 0;
10+
int n = intervals.length;
11+
12+
while (i < n && intervals[i][1] < newInterval[0]) {
13+
output.add(intervals[i]);
14+
i += 1;
15+
}
16+
17+
while (i < n && intervals[i][0] <= newInterval[1]) {
18+
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
19+
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
20+
i += 1;
21+
}
22+
23+
output.add(newInterval);
24+
25+
while (i < n) {
26+
output.add(intervals[i]);
27+
i += 1;
28+
}
29+
30+
return output.toArray(new int[output.size()][]);
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TC: O(n)
2+
// visiting all nodes
3+
// SC: O(1)
4+
// constant space complexity
5+
class Solution {
6+
public int maxDepth(TreeNode root) {
7+
return getMax(root, 0);
8+
}
9+
10+
private int getMax(TreeNode node, int depth) {
11+
if (node == null) return depth;
12+
depth += 1;
13+
return Math.max(getMax(node.left, depth), getMax(node.right, depth));
14+
}
15+
}

reorder-list/TonyKim9401.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// TC: O(n)
2+
// -> find middle, reverse, merge -> max O(n)
3+
// SC: O(1)
4+
class Solution {
5+
public void reorderList(ListNode head) {
6+
if (head == null || head.next == null) return;
7+
8+
ListNode slow = head, fast = head;
9+
while (fast != null && fast.next != null) {
10+
slow = slow.next;
11+
fast = fast.next.next;
12+
}
13+
14+
ListNode backSide = null;
15+
ListNode curr = slow.next;
16+
slow.next = null;
17+
18+
while (curr != null) {
19+
ListNode temp = curr.next;
20+
curr.next = backSide;
21+
backSide = curr;
22+
curr = temp;
23+
}
24+
25+
ListNode first = head;
26+
ListNode second = backSide;
27+
while (second != null) {
28+
ListNode temp = first.next;
29+
first.next = second;
30+
first = second;
31+
second = temp;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)