From f5f1c9cb9f3eda12aaae3d5f622de6c9778ff0bf Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 2 Feb 2025 16:44:20 +0900 Subject: [PATCH 1/7] minimum window substring --- minimum-window-substring/eunhwa99.java | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 minimum-window-substring/eunhwa99.java diff --git a/minimum-window-substring/eunhwa99.java b/minimum-window-substring/eunhwa99.java new file mode 100644 index 000000000..401a8884d --- /dev/null +++ b/minimum-window-substring/eunhwa99.java @@ -0,0 +1,42 @@ +class Solution { + + // TP: O(N+M) + // SP: O(1) + public String minWindow(String s, String t) { + int[] tFreq = new int[128]; + for (char c : t.toCharArray()) { + tFreq[c]++; + } + + int left = 0; + int right = 0; + int minLen = Integer.MAX_VALUE; + int minLeft = 0; + int count = 0; + int tLen = t.length(); + + while (right < s.length()) { + if (tFreq[s.charAt(right++)]-- > 0) { // tFreq[s.charAt(right++)]-- > 0 -> s.charAt(right) is in t + count++; // s의 문자가 t에 있기 때문에 count를 증가시킨다. + } + + if (count == tLen) { + // 아래 while문은 left를 옮기면서 s의 문자가 t에 있는지 확인한다. (최소 길이를 찾기 위해) + while (left < right && tFreq[s.charAt(left)] < 0) { // tFreq[s.charAt(left)] < 0 -> s.charAt(left) is not in t + tFreq[s.charAt(left++)]++; + } + + if (right - left < minLen) { + minLen = right - left; + minLeft = left; + } + tFreq[s.charAt(left++)]++; // left 한 칸 올리기 + count--; + } + + } + + return minLen == Integer.MAX_VALUE ? "" : s.substring(minLeft, minLeft + minLen); + + } +} From 04f7c0ef5c3377e6530ae3f8874197aad29db7b9 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 9 Feb 2025 09:36:53 +0900 Subject: [PATCH 2/7] invert binary tree --- invert-binary-tree/eunhwa99.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 invert-binary-tree/eunhwa99.java diff --git a/invert-binary-tree/eunhwa99.java b/invert-binary-tree/eunhwa99.java new file mode 100644 index 000000000..0810b65c2 --- /dev/null +++ b/invert-binary-tree/eunhwa99.java @@ -0,0 +1,18 @@ +class Solution { + + public TreeNode invertTree(TreeNode root) { + if (root == null) { + return null; + } + + TreeNode tmp = root.left; + root.left = root.right; + root.right = tmp; + + invertTree(root.right); + invertTree(root.left); + + return root; + } + +} From af3329711a58d613560ea77d1317b45cefb8f490 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 9 Feb 2025 10:04:03 +0900 Subject: [PATCH 3/7] =?UTF-8?q?invert=20binary=20tree:=20TC,=20SC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- invert-binary-tree/eunhwa99.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/invert-binary-tree/eunhwa99.java b/invert-binary-tree/eunhwa99.java index 0810b65c2..aa0b0ef42 100644 --- a/invert-binary-tree/eunhwa99.java +++ b/invert-binary-tree/eunhwa99.java @@ -1,5 +1,7 @@ class Solution { + // TC : O(N) + // SC : O(1) public TreeNode invertTree(TreeNode root) { if (root == null) { return null; @@ -16,3 +18,4 @@ public TreeNode invertTree(TreeNode root) { } } + From 183e3d2cadb401d8dddeb2a88e94d96e112eeff9 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 9 Feb 2025 10:07:59 +0900 Subject: [PATCH 4/7] search in rotated sorted array --- search-in-rotated-sorted-array/eunhwa99.java | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 search-in-rotated-sorted-array/eunhwa99.java diff --git a/search-in-rotated-sorted-array/eunhwa99.java b/search-in-rotated-sorted-array/eunhwa99.java new file mode 100644 index 000000000..b2168ffee --- /dev/null +++ b/search-in-rotated-sorted-array/eunhwa99.java @@ -0,0 +1,35 @@ +class Solution { + + // TC : O(logN) + // SC : O(1) + public int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + return mid; + } + + if (nums[left] <= nums[mid]) { + if (nums[left] <= target && target < nums[mid]) { + right = mid - 1; + } else { + left = mid + 1; + } + } else { + if (nums[mid] < target && target <= nums[right]) { + left = mid - 1; + } else { + right = mid + 1; + } + } + + } + + return -1; + } +} + + From f8e60a220f6b3468095f5e13e6317ef84d16a13b Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Wed, 12 Feb 2025 21:44:23 +0900 Subject: [PATCH 5/7] courses schedule --- course-schedule/eunhwa99.java | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 course-schedule/eunhwa99.java diff --git a/course-schedule/eunhwa99.java b/course-schedule/eunhwa99.java new file mode 100644 index 000000000..9d09e3946 --- /dev/null +++ b/course-schedule/eunhwa99.java @@ -0,0 +1,45 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +class Solution { + + // TC: O(numCourses + E) E는 prerequisites 배열의 길이 (즉, 간선의 개수). + // SC: O(numCourses + E) + public boolean canFinish(int numCourses, int[][] prerequisites) { + int[] inDegree = new int[numCourses]; + List> adj = new ArrayList<>(); + + for (int i = 0; i < numCourses; i++) { + adj.add(new ArrayList<>()); + } + + for (int[] pre : prerequisites) { + adj.get(pre[1]).add(pre[0]); + inDegree[pre[0]]++; + } + + Queue queue = new LinkedList<>(); + for (int i = 0; i < numCourses; i++) { + if (inDegree[i] == 0) { + queue.add(i); + } + } + for (int i = 0; i < numCourses; i++) { + if (queue.isEmpty()) { + return false; + } + int course = queue.poll(); + + for (int nextCourse : adj.get(course)) { + inDegree[nextCourse]--; + if (inDegree[nextCourse] == 0) { + queue.add(nextCourse); + } + } + } + return queue.isEmpty(); + } +} + From 441e43af2327b23b0a993e4bae8fabf2c69a5a7e Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Thu, 13 Feb 2025 21:34:46 +0900 Subject: [PATCH 6/7] jump game --- jump-game/eunhwa99.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 jump-game/eunhwa99.java diff --git a/jump-game/eunhwa99.java b/jump-game/eunhwa99.java new file mode 100644 index 000000000..b70e5cb6d --- /dev/null +++ b/jump-game/eunhwa99.java @@ -0,0 +1,14 @@ +class Solution { + + public boolean canJump(int[] nums) { + int furthestIndex = 0; + for (int i = 0; i < nums.length; i++) { + if (furthestIndex < i) { + return false; + } + + furthestIndex = Math.max(furthestIndex, i + nums[i]); + } + return true; + } +} From f3570004e9827a722158aa739378fad55b9a778a Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Fri, 14 Feb 2025 20:16:13 +0900 Subject: [PATCH 7/7] merged k sorted lists --- merge-k-sorted-lists/eunhwa99.java | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 merge-k-sorted-lists/eunhwa99.java diff --git a/merge-k-sorted-lists/eunhwa99.java b/merge-k-sorted-lists/eunhwa99.java new file mode 100644 index 000000000..9035ae715 --- /dev/null +++ b/merge-k-sorted-lists/eunhwa99.java @@ -0,0 +1,35 @@ +import java.util.PriorityQueue; + +/** + * Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {} + * ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; + * this.next = next; } } + */ + +// TC : PQ -> O(NlogN) + // SC: Linked list -> O(N) +class Solution { + + public ListNode mergeKLists(ListNode[] lists) { + if (lists == null || lists.length == 0) { + return null; + } + PriorityQueue pq = new PriorityQueue<>(); + + for (int i = 0; i < lists.length; i++) { + while (lists[i] != null) { + pq.add(lists[i].val); + lists[i] = lists[i].next; + } + } + ListNode dummy = new ListNode(0); + ListNode current = dummy; + while (!pq.isEmpty()) { + current.next = new ListNode(pq.poll()); + current = current.next; + } + + return dummy.next; + } +} +