From 62fca0d4d566c63d90799372aa1223679e4c3e6c Mon Sep 17 00:00:00 2001 From: ktony Date: Mon, 4 Nov 2024 21:21:11 -0500 Subject: [PATCH 1/6] Meeting Rooms --- meeting-rooms/TonyKim9401.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 meeting-rooms/TonyKim9401.java diff --git a/meeting-rooms/TonyKim9401.java b/meeting-rooms/TonyKim9401.java new file mode 100644 index 000000000..341b94f85 --- /dev/null +++ b/meeting-rooms/TonyKim9401.java @@ -0,0 +1,17 @@ +// TC: O(n log n) +// list sort +// SC: O(n) +// list sort max use O(n) +public class Solution { + public boolean canAttendMeetings(List intervals) { + intervals.sort(Comparator.comparingInt(o -> o.start)); + + for (int i = 0; i < intervals.size() - 1; i++) { + Interval preInterval = intervals.get(i); + Interval postInterval = intervals.get(i+1); + + if (preInterval.end > postInterval.start) return false; + } + return true; + } +} From 556a2282567a279a3f2727afa58df11d8b0be9d6 Mon Sep 17 00:00:00 2001 From: ktony Date: Tue, 5 Nov 2024 19:54:15 -0500 Subject: [PATCH 2/6] Lowest Common Ancestor Of A Binary Search Tree --- .../TonyKim9401.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java diff --git a/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java b/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java new file mode 100644 index 000000000..61abb9be2 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java @@ -0,0 +1,31 @@ +// TC: O(n) +// need to check all nodes +// SC: O(h) +// h can be O(n) in the worst case, O(log n) in the best case +class Solution { + public TreeNode lcaDeepestLeaves(TreeNode root) { + return dfs(root, 0).node; + } + + private ResultNode dfs(TreeNode node, int depth) { + if (node == null) return new ResultNode(null, depth); + + depth += 1; + ResultNode leftNode = dfs(node.left, depth); + ResultNode rightNode = dfs(node.right, depth); + + if (leftNode.depth == rightNode.depth) return new ResultNode(node, leftNode.depth); + else if (leftNode.depth > rightNode.depth) return leftNode; + else return rightNode; + } + + private class ResultNode { + private TreeNode node; + private int depth; + + ResultNode(TreeNode node, int depth) { + this.node = node; + this.depth = depth; + } + } +} From 4c325d260e051715fc056273aac90c0414756067 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 6 Nov 2024 14:31:40 -0500 Subject: [PATCH 3/6] House Robber --- house-robber/TonyKim9401.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 house-robber/TonyKim9401.java diff --git a/house-robber/TonyKim9401.java b/house-robber/TonyKim9401.java new file mode 100644 index 000000000..6be1011df --- /dev/null +++ b/house-robber/TonyKim9401.java @@ -0,0 +1,21 @@ +// TC: O(n) +// always need to check every case +// SC: O(n) +// the length of the result int list is same with the length of the given nums int list +class Solution { + public int rob(int[] nums) { + int[] result = new int[nums.length]; + + if (nums.length < 2) return nums[0]; + if (nums.length < 3) return Math.max(nums[0], nums[1]); + + result[0] = nums[0]; + result[1] = Math.max(nums[0], nums[1]); + + for (int i = 2; i < nums.length; i++) { + result[i] = Math.max(result[i - 1], result[i - 2] + nums[i]); + } + + return result[nums.length-1]; + } +} From f277d32e0c2d5b45baa1ccee00e2c436f51eea96 Mon Sep 17 00:00:00 2001 From: ktony Date: Fri, 8 Nov 2024 00:40:52 -0500 Subject: [PATCH 4/6] Non Overlapping Intervals --- non-overlapping-intervals/TonyKim9401.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 non-overlapping-intervals/TonyKim9401.java diff --git a/non-overlapping-intervals/TonyKim9401.java b/non-overlapping-intervals/TonyKim9401.java new file mode 100644 index 000000000..11df1c2d1 --- /dev/null +++ b/non-overlapping-intervals/TonyKim9401.java @@ -0,0 +1,21 @@ +// TC: O(n log n) +// in order to order the given intervals array +// SC: O(1) +// only constant space necessary +class Solution { + public int eraseOverlapIntervals(int[][] intervals) { + Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[1], o2[1])); + + int output = 0; + int end = intervals[0][1]; + + for (int i = 1; i < intervals.length; i++) { + int[] currentInterval = intervals[i]; + + if (currentInterval[0] < end) output += 1; + else end = currentInterval[1]; + } + + return output; + } +} From 68c9451a7c93a3fd980e66be9c1a2f54800479c8 Mon Sep 17 00:00:00 2001 From: ktony Date: Fri, 8 Nov 2024 22:07:14 -0500 Subject: [PATCH 5/6] Lowest Common Ancestor Of A Binary Search Tree --- .../TonyKim9401.java | 40 +++++-------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java b/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java index 61abb9be2..17f451a2d 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java +++ b/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java @@ -1,31 +1,13 @@ -// TC: O(n) -// need to check all nodes -// SC: O(h) -// h can be O(n) in the worst case, O(log n) in the best case +// TC: O(h) +// h = the high of binary search tree +// SC: O(1) +// Doesn't require additional space class Solution { - public TreeNode lcaDeepestLeaves(TreeNode root) { - return dfs(root, 0).node; + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (p.val < root.val && q.val < root.val) + return lowestCommonAncestor(root.left, p, q); + if (p.val > root.val && q.val > root.val) + return lowestCommonAncestor(root.right, p, q); + return root; } - - private ResultNode dfs(TreeNode node, int depth) { - if (node == null) return new ResultNode(null, depth); - - depth += 1; - ResultNode leftNode = dfs(node.left, depth); - ResultNode rightNode = dfs(node.right, depth); - - if (leftNode.depth == rightNode.depth) return new ResultNode(node, leftNode.depth); - else if (leftNode.depth > rightNode.depth) return leftNode; - else return rightNode; - } - - private class ResultNode { - private TreeNode node; - private int depth; - - ResultNode(TreeNode node, int depth) { - this.node = node; - this.depth = depth; - } - } -} +} \ No newline at end of file From 0a0445b20f1a28e0648706c3fcbc698f1569299f Mon Sep 17 00:00:00 2001 From: ktony Date: Fri, 8 Nov 2024 22:07:31 -0500 Subject: [PATCH 6/6] Lowest Common Ancestor Of A Binary Search Tree --- lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java b/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java index 17f451a2d..da377becc 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java +++ b/lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java @@ -10,4 +10,4 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { return lowestCommonAncestor(root.right, p, q); return root; } -} \ No newline at end of file +}