From be7b1c80582197264be3e2224cd1ed8d7dbac39f Mon Sep 17 00:00:00 2001 From: ktony Date: Mon, 11 Nov 2024 00:20:48 -0500 Subject: [PATCH 1/4] Reverse Bits --- reverse-bits/TonyKim9401.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 reverse-bits/TonyKim9401.java diff --git a/reverse-bits/TonyKim9401.java b/reverse-bits/TonyKim9401.java new file mode 100644 index 000000000..a1d107579 --- /dev/null +++ b/reverse-bits/TonyKim9401.java @@ -0,0 +1,14 @@ +public class Solution { + public int reverseBits(int n) { + // time complexity O(1) + // space complexity O(1) + int output = 0; + + for (int i = 0; i < Integer.SIZE; i++) { + output <<= 1; + output += n & 1; + n >>= 1; + } + return output; + } +} From 7f781840d18c7f8d56fedda58856e7f9082fb03b Mon Sep 17 00:00:00 2001 From: ktony Date: Mon, 11 Nov 2024 12:54:52 -0500 Subject: [PATCH 2/4] Binary Tree Level Order Traversal --- .../TonyKim9401.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 binary-tree-level-order-traversal/TonyKim9401.java diff --git a/binary-tree-level-order-traversal/TonyKim9401.java b/binary-tree-level-order-traversal/TonyKim9401.java new file mode 100644 index 000000000..4ec55bd91 --- /dev/null +++ b/binary-tree-level-order-traversal/TonyKim9401.java @@ -0,0 +1,26 @@ +// TC: O(n) +// need to visit all nodes +// SC: O(n) +// normally O(log n) required however, O(n) in the worst case +class Solution { + private List> output = new ArrayList<>(); + public List> levelOrder(TreeNode root) { + dsf(0, root); + return output; + } + + private void dsf(int level, TreeNode node) { + if (node == null) return; + + if (output.size() == level) { + List inside = new ArrayList<>(); + inside.add(node.val); + output.add(inside); + } else { + output.get(level).add(node.val); + } + level += 1; + dsf(level, node.left); + dsf(level, node.right); + } +} From f6027a786377e5039d9766a08f95770c37880180 Mon Sep 17 00:00:00 2001 From: ktony Date: Tue, 12 Nov 2024 12:29:06 -0500 Subject: [PATCH 3/4] House Robber II & Type --- .../TonyKim9401.java | 8 ++--- house-robber-ii/TonyKim9401.java | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 house-robber-ii/TonyKim9401.java diff --git a/binary-tree-level-order-traversal/TonyKim9401.java b/binary-tree-level-order-traversal/TonyKim9401.java index 4ec55bd91..127b79377 100644 --- a/binary-tree-level-order-traversal/TonyKim9401.java +++ b/binary-tree-level-order-traversal/TonyKim9401.java @@ -5,11 +5,11 @@ class Solution { private List> output = new ArrayList<>(); public List> levelOrder(TreeNode root) { - dsf(0, root); + dfs(0, root); return output; } - private void dsf(int level, TreeNode node) { + private void dfs(int level, TreeNode node) { if (node == null) return; if (output.size() == level) { @@ -20,7 +20,7 @@ private void dsf(int level, TreeNode node) { output.get(level).add(node.val); } level += 1; - dsf(level, node.left); - dsf(level, node.right); + dfs(level, node.left); + dfs(level, node.right); } } diff --git a/house-robber-ii/TonyKim9401.java b/house-robber-ii/TonyKim9401.java new file mode 100644 index 000000000..87c047264 --- /dev/null +++ b/house-robber-ii/TonyKim9401.java @@ -0,0 +1,32 @@ +// TC: O(n) +// visit all nums at least once +// SC: O(1) +// only constant memory space required +class Solution { + public int rob(int[] nums) { + if (nums.length == 1) return nums[0]; + + int prev = 0; + int post = 0; + int output1 = 0; + + for (int i = 0; i < nums.length - 1; i++) { + int temp = prev; + prev = Math.max(post + nums[i], prev); + post = temp; + } + output1 = prev; + + prev = 0; + post = 0; + int output2 = 0; + for (int i = 1; i < nums.length; i++) { + int temp = prev; + prev = Math.max(post + nums[i], prev); + post = temp; + } + output2 = prev; + + return Math.max(output1, output2); + } +} From b66b35ead7e4c199372e8f63cbcda08a9e1e9704 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 13 Nov 2024 21:51:24 -0500 Subject: [PATCH 4/4] Meeting Rooms II --- meeting-rooms-ii/TonyKim9401.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 meeting-rooms-ii/TonyKim9401.java diff --git a/meeting-rooms-ii/TonyKim9401.java b/meeting-rooms-ii/TonyKim9401.java new file mode 100644 index 000000000..e58e18540 --- /dev/null +++ b/meeting-rooms-ii/TonyKim9401.java @@ -0,0 +1,22 @@ +// TC: O(n) +// visit all intervals to compare each of start time +// SC: O(n) +// PQ save all intervals in the worst case +public class Solution { + public int minMeetingRooms(List intervals) { + if (intervals == null || intervals.isEmpty()) return 0; + + intervals.sort((a, b) -> a.start - b.start); + + PriorityQueue endTimes = new PriorityQueue<>(); + endTimes.add(intervals.get(0).end); + + for (int i = 1; i < intervals.size(); i++) { + Interval current = intervals.get(i); + if (current.start >= endTimes.peek()) endTimes.poll(); + endTimes.add(current.end); + } + + return endTimes.size(); + } +}