Skip to content

Commit

Permalink
Merge pull request #1006 from eunhwa99/main
Browse files Browse the repository at this point in the history
[eunhwa99] Week 10
  • Loading branch information
eunhwa99 authored Feb 15, 2025
2 parents d2782d4 + f357000 commit 02e4d9a
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 0 deletions.
45 changes: 45 additions & 0 deletions course-schedule/eunhwa99.java
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();
}
}

21 changes: 21 additions & 0 deletions invert-binary-tree/eunhwa99.java
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;
}

}

14 changes: 14 additions & 0 deletions jump-game/eunhwa99.java
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;
}
}
35 changes: 35 additions & 0 deletions merge-k-sorted-lists/eunhwa99.java
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;
}
}

42 changes: 42 additions & 0 deletions minimum-window-substring/eunhwa99.java
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);

}
}
35 changes: 35 additions & 0 deletions search-in-rotated-sorted-array/eunhwa99.java
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;
}
}


0 comments on commit 02e4d9a

Please sign in to comment.