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(); + } +} + diff --git a/invert-binary-tree/eunhwa99.java b/invert-binary-tree/eunhwa99.java new file mode 100644 index 000000000..aa0b0ef42 --- /dev/null +++ b/invert-binary-tree/eunhwa99.java @@ -0,0 +1,21 @@ +class Solution { + + // TC : O(N) + // SC : O(1) + 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; + } + +} + 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; + } +} 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; + } +} + 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); + + } +} 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; + } +} + +