-
-
Notifications
You must be signed in to change notification settings - Fork 195
[eunhwa99] Week 10 #1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[eunhwa99] Week 10 #1006
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f5f1c9c
minimum window substring
04f7c0e
invert binary tree
6a624af
Merge remote-tracking branch 'origin/main'
af33297
invert binary tree: TC, SC 추가
183e3d2
search in rotated sorted array
f8e60a2
courses schedule
441e43a
jump game
f357000
merged k sorted lists
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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]; | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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; | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.