From 4a85b9ee7965bc5ebb38b284ddfd17524d3fe4f8 Mon Sep 17 00:00:00 2001 From: ktony Date: Mon, 21 Oct 2024 13:30:30 -0400 Subject: [PATCH 1/5] Maximum Depth Of Binary Tree --- maximum-depth-of-binary-tree/TonyKim9401.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 maximum-depth-of-binary-tree/TonyKim9401.java diff --git a/maximum-depth-of-binary-tree/TonyKim9401.java b/maximum-depth-of-binary-tree/TonyKim9401.java new file mode 100644 index 000000000..257b51ae5 --- /dev/null +++ b/maximum-depth-of-binary-tree/TonyKim9401.java @@ -0,0 +1,15 @@ +// TC: O(n) +// visiting all nodes +// SC: O(1) +// constant space complexity +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)); + } +} From 0f78ecde1905b200e1b7cca160f398852b05ffdc Mon Sep 17 00:00:00 2001 From: ktony Date: Tue, 22 Oct 2024 12:26:35 -0400 Subject: [PATCH 2/5] Reorder List --- reorder-list/TonyKim9401.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 reorder-list/TonyKim9401.java diff --git a/reorder-list/TonyKim9401.java b/reorder-list/TonyKim9401.java new file mode 100644 index 000000000..47fc8e4b2 --- /dev/null +++ b/reorder-list/TonyKim9401.java @@ -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; + } + } +} From 094e739a9fce41c99643c5a921440112d9df3607 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 23 Oct 2024 13:43:06 -0400 Subject: [PATCH 3/5] Graph Valid Tree --- graph-valid-tree/TonyKim9401.java | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 graph-valid-tree/TonyKim9401.java diff --git a/graph-valid-tree/TonyKim9401.java b/graph-valid-tree/TonyKim9401.java new file mode 100644 index 000000000..aaaf354cd --- /dev/null +++ b/graph-valid-tree/TonyKim9401.java @@ -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> 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> 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; + } +} From b8b8ed57d8e883eae3a1709da7727a54fd67e248 Mon Sep 17 00:00:00 2001 From: ktony Date: Thu, 24 Oct 2024 08:55:09 -0400 Subject: [PATCH 4/5] Insert Interval --- insert-interval/TonyKim9401.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 insert-interval/TonyKim9401.java diff --git a/insert-interval/TonyKim9401.java b/insert-interval/TonyKim9401.java new file mode 100644 index 000000000..aa13d3513 --- /dev/null +++ b/insert-interval/TonyKim9401.java @@ -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 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()][]); + } +} From c243ab397e80bbd533073fcf8f3b376bbaa1110a Mon Sep 17 00:00:00 2001 From: ktony Date: Thu, 24 Oct 2024 18:59:34 -0400 Subject: [PATCH 5/5] Binary Tree Maximum Path Sum --- binary-tree-maximum-path-sum/TonyKim9401.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 binary-tree-maximum-path-sum/TonyKim9401.java diff --git a/binary-tree-maximum-path-sum/TonyKim9401.java b/binary-tree-maximum-path-sum/TonyKim9401.java new file mode 100644 index 000000000..db69e7a05 --- /dev/null +++ b/binary-tree-maximum-path-sum/TonyKim9401.java @@ -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); + } +}