-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1006 from eunhwa99/main
[eunhwa99] Week 10
- Loading branch information
Showing
6 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<Integer>> 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<Integer> 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(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> 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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
|