From 720398a2e132bfb8a25618ec5236f9cf15553b6c Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 13 Oct 2024 23:11:57 -0400 Subject: [PATCH 1/5] Invert Binary Tree --- invert-binary-tree/TonyKim9401.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 invert-binary-tree/TonyKim9401.java diff --git a/invert-binary-tree/TonyKim9401.java b/invert-binary-tree/TonyKim9401.java new file mode 100644 index 000000000..878b68bb7 --- /dev/null +++ b/invert-binary-tree/TonyKim9401.java @@ -0,0 +1,16 @@ +// TC: O(n) +// -> visit all nodes to invert +// SC: O(n) +// -> create all nodes again to exchange +class Solution { + public TreeNode invertTree(TreeNode root) { + if (root == null) return null; + invertTree(root.left); + invertTree(root.right); + TreeNode left = root.left; + TreeNode right = root.right; + root.left = right; + root.right = left; + return root; + } +} From 891b6c0b1f2c28ab80798e845cc076139cd2a2af Mon Sep 17 00:00:00 2001 From: ktony Date: Mon, 14 Oct 2024 13:38:23 -0400 Subject: [PATCH 2/5] Search In Rotated Sorted Array --- .../TonyKim9401.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 search-in-rotated-sorted-array/TonyKim9401.java diff --git a/search-in-rotated-sorted-array/TonyKim9401.java b/search-in-rotated-sorted-array/TonyKim9401.java new file mode 100644 index 000000000..e2d145cf1 --- /dev/null +++ b/search-in-rotated-sorted-array/TonyKim9401.java @@ -0,0 +1,25 @@ +// TC: O(log n) +// -> binary search +// SC: O(1) +class Solution { + public int search(int[] nums, int target) { + + int start = 0; + int end = nums.length - 1; + + while (start <= end) { + int mid = start + (end - start) / 2; + + if (nums[mid] == target) return mid; + + if (nums[start] <= nums[mid]) { + if (nums[start] <= target && target < nums[mid]) end = mid - 1; + else start = mid + 1; + } else { + if (nums[mid] < target && target <= nums[end]) start = mid + 1; + else end = mid - 1; + } + } + return -1; + } +} From ad63f81c72bb8affee397d446c72c63bf2c1a2d7 Mon Sep 17 00:00:00 2001 From: ktony Date: Thu, 17 Oct 2024 20:22:56 -0400 Subject: [PATCH 3/5] Course Schedule --- course-schedule/TonyKim9401.java | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 course-schedule/TonyKim9401.java diff --git a/course-schedule/TonyKim9401.java b/course-schedule/TonyKim9401.java new file mode 100644 index 000000000..edbff43de --- /dev/null +++ b/course-schedule/TonyKim9401.java @@ -0,0 +1,37 @@ +// TC: O(n + p) +// n -> number of courses, p -> the length of prerequisites +// SC: O(n + m) +// n -> the length of the graph size, m -> the length of nested list's size +class Solution { + public boolean canFinish(int numCourses, int[][] prerequisites) { + List> graph = new ArrayList<>(); + int[] inDegree = new int[numCourses]; + + for (int i = 0; i < numCourses; i++) graph.add(new ArrayList<>()); + + for (int[] prerequisite : prerequisites) { + int course = prerequisite[0]; + int pre = prerequisite[1]; + graph.get(pre).add(course); + inDegree[course] += 1; + } + + Queue q = new LinkedList<>(); + for (int i = 0; i < numCourses; i++) { + if (inDegree[i] == 0) q.offer(i); + } + + int visitedCourses = 0; + while (!q.isEmpty()) { + int course = q.poll(); + visitedCourses += 1; + + for (int nextCourse : graph.get(course)) { + inDegree[nextCourse] -= 1; + if (inDegree[nextCourse] == 0) q.offer(nextCourse); + } + } + + return visitedCourses == numCourses; + } +} From 1c60fed74c423f532162e419ab8d3316910f22cf Mon Sep 17 00:00:00 2001 From: ktony Date: Thu, 17 Oct 2024 21:12:41 -0400 Subject: [PATCH 4/5] Jump Game --- jump-game/TonyKim9401.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 jump-game/TonyKim9401.java diff --git a/jump-game/TonyKim9401.java b/jump-game/TonyKim9401.java new file mode 100644 index 000000000..7df98f72c --- /dev/null +++ b/jump-game/TonyKim9401.java @@ -0,0 +1,13 @@ +// TC: O(n) +// SC: O(1) +class Solution { + public boolean canJump(int[] nums) { + int jump = 0; + + for (int i = 0; i < nums.length; i++) { + if (i > jump) return false; + jump = Math.max(jump, i + nums[i]); + } + return true; + } +} From 19c3560cbb7940aa0df9f57f0c6f5070d81c8986 Mon Sep 17 00:00:00 2001 From: ktony Date: Fri, 18 Oct 2024 00:38:02 -0400 Subject: [PATCH 5/5] Merge K Sorted Lists --- merge-k-sorted-lists/TonyKim9401.java | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 merge-k-sorted-lists/TonyKim9401.java diff --git a/merge-k-sorted-lists/TonyKim9401.java b/merge-k-sorted-lists/TonyKim9401.java new file mode 100644 index 000000000..ac513085c --- /dev/null +++ b/merge-k-sorted-lists/TonyKim9401.java @@ -0,0 +1,30 @@ +// TC: O(n * log m) +// m -> the number of the lists, n -> the number of node +// SC: O(m) +// m -> the number of the lists (max m) +class Solution { + public ListNode mergeKLists(ListNode[] lists) { + if (lists == null || lists.length == 0) return null; + + PriorityQueue pq = new PriorityQueue<>((a, b) -> a.val - b.val); + + for (ListNode node : lists) { + if (node != null) pq.offer(node); + } + + ListNode dummy = new ListNode(0); + ListNode current = dummy; + + while (!pq.isEmpty()) { + ListNode minNode = pq.poll(); + current.next = minNode; + current = current.next; + + if (minNode.next != null) { + pq.offer(minNode.next); + } + } + + return dummy.next; + } +}