Skip to content

[sora0319] WEEK 11 solutions #1583

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 5 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions binary-tree-maximum-path-sum/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Solution {
private int maxSum;

public int maxPathSum(TreeNode root) {
maxSum = root.val;
dfs(root);
return maxSum;
}

private int dfs(TreeNode node) {
if (node == null) {
return 0;
}

int leftMax = Math.max(dfs(node.left), 0);
int rightMax = Math.max(dfs(node.right), 0);

maxSum = Math.max(maxSum, node.val + leftMax + rightMax);

return node.val + Math.max(leftMax, rightMax);
}
}

40 changes: 40 additions & 0 deletions graph-valid-tree/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class Solution {
public boolean validTree(int n, int[][] edges) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int i = 0; i < n; i++) {
graph.put(i, new ArrayList<>());
}

for (int[] edge : edges) {
int node = edge[0];
int adj = edge[1];
graph.get(node).add(adj);
graph.get(adj).add(node);
}

Set<Integer> visited = new HashSet<>();
if (inCycle(0, -1, graph, visited)) {
return false;
}

return visited.size() == n;
}

private boolean inCycle(int node, int prev, Map<Integer, List<Integer>> graph, Set<Integer> visited) {
if (visited.contains(node)) {
return true;
}

visited.add(node);

for (int neighbor : graph.get(node)) {
if (neighbor == prev) continue;
if (inCycle(neighbor, node, graph, visited)) {
return true;
}
}

return false;
}
}

18 changes: 18 additions & 0 deletions merge-intervals/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Solution {
public int[][] merge(int[][] intervals) {
List<int[]> output = new ArrayList<>();

Arrays.sort(intervals, (a, b) -> a[0]- b[0]);

for (int[] interval : intervals) {
if (output.isEmpty() || output.get(output.size() - 1)[1] < interval[0]) {
output.add(interval);
} else {
output.get(output.size() - 1)[1] = Math.max(output.get(output.size() - 1)[1], interval[1]);
}
}

return output.toArray(new int[output.size()][]);
}
}

20 changes: 20 additions & 0 deletions missing-number/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int missingNumber(int[] nums) {
int result = 0;
int[] counts = new int[nums.length+1];

for(int n : nums){
counts[n] = 1;
}

for(int i = 0; i < counts.length; i++){
if(counts[i] == 0){
result = i;
break;
}
}

return result;
}
}

38 changes: 38 additions & 0 deletions reorder-list/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;

ListNode backward = head;
ListNode forward = head;
while (forward != null && forward.next != null) {
backward = backward.next;
forward = forward.next.next;
}

ListNode curr = backward.next;
backward.next = null;

ListNode prev = null;
while (curr != null) {
ListNode tempNext = curr.next;
curr.next = prev;
prev = curr;
curr = tempNext;
}

ListNode first = head;
ListNode second = prev;

while (second != null) {
ListNode firstNext = first.next;
ListNode secondNext = second.next;

first.next = second;
second.next = firstNext;

first = firstNext;
second = secondNext;
}
}
}