Skip to content

Commit 936510f

Browse files
authored
Merge pull request #1583 from sora0319/main
[sora0319] WEEK 11 solutions
2 parents a37ad35 + bf6bdeb commit 936510f

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
private int maxSum;
3+
4+
public int maxPathSum(TreeNode root) {
5+
maxSum = root.val;
6+
dfs(root);
7+
return maxSum;
8+
}
9+
10+
private int dfs(TreeNode node) {
11+
if (node == null) {
12+
return 0;
13+
}
14+
15+
int leftMax = Math.max(dfs(node.left), 0);
16+
int rightMax = Math.max(dfs(node.right), 0);
17+
18+
maxSum = Math.max(maxSum, node.val + leftMax + rightMax);
19+
20+
return node.val + Math.max(leftMax, rightMax);
21+
}
22+
}
23+

graph-valid-tree/sora0319.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Solution {
2+
public boolean validTree(int n, int[][] edges) {
3+
Map<Integer, List<Integer>> graph = new HashMap<>();
4+
for (int i = 0; i < n; i++) {
5+
graph.put(i, new ArrayList<>());
6+
}
7+
8+
for (int[] edge : edges) {
9+
int node = edge[0];
10+
int adj = edge[1];
11+
graph.get(node).add(adj);
12+
graph.get(adj).add(node);
13+
}
14+
15+
Set<Integer> visited = new HashSet<>();
16+
if (inCycle(0, -1, graph, visited)) {
17+
return false;
18+
}
19+
20+
return visited.size() == n;
21+
}
22+
23+
private boolean inCycle(int node, int prev, Map<Integer, List<Integer>> graph, Set<Integer> visited) {
24+
if (visited.contains(node)) {
25+
return true;
26+
}
27+
28+
visited.add(node);
29+
30+
for (int neighbor : graph.get(node)) {
31+
if (neighbor == prev) continue;
32+
if (inCycle(neighbor, node, graph, visited)) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
}
40+

merge-intervals/sora0319.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int[][] merge(int[][] intervals) {
3+
List<int[]> output = new ArrayList<>();
4+
5+
Arrays.sort(intervals, (a, b) -> a[0]- b[0]);
6+
7+
for (int[] interval : intervals) {
8+
if (output.isEmpty() || output.get(output.size() - 1)[1] < interval[0]) {
9+
output.add(interval);
10+
} else {
11+
output.get(output.size() - 1)[1] = Math.max(output.get(output.size() - 1)[1], interval[1]);
12+
}
13+
}
14+
15+
return output.toArray(new int[output.size()][]);
16+
}
17+
}
18+

missing-number/sora0319.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int missingNumber(int[] nums) {
3+
int result = 0;
4+
int[] counts = new int[nums.length+1];
5+
6+
for(int n : nums){
7+
counts[n] = 1;
8+
}
9+
10+
for(int i = 0; i < counts.length; i++){
11+
if(counts[i] == 0){
12+
result = i;
13+
break;
14+
}
15+
}
16+
17+
return result;
18+
}
19+
}
20+

reorder-list/sora0319.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class Solution {
2+
public void reorderList(ListNode head) {
3+
if (head == null || head.next == null) return;
4+
5+
ListNode backward = head;
6+
ListNode forward = head;
7+
while (forward != null && forward.next != null) {
8+
backward = backward.next;
9+
forward = forward.next.next;
10+
}
11+
12+
ListNode curr = backward.next;
13+
backward.next = null;
14+
15+
ListNode prev = null;
16+
while (curr != null) {
17+
ListNode tempNext = curr.next;
18+
curr.next = prev;
19+
prev = curr;
20+
curr = tempNext;
21+
}
22+
23+
ListNode first = head;
24+
ListNode second = prev;
25+
26+
while (second != null) {
27+
ListNode firstNext = first.next;
28+
ListNode secondNext = second.next;
29+
30+
first.next = second;
31+
second.next = firstNext;
32+
33+
first = firstNext;
34+
second = secondNext;
35+
}
36+
}
37+
}
38+

0 commit comments

Comments
 (0)