Skip to content

Commit cd10345

Browse files
authored
Merge pull request #534 from TonyKim9401/main
[Tony] WEEK 10 Solutions
2 parents 3c980d6 + 19c3560 commit cd10345

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

course-schedule/TonyKim9401.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// TC: O(n + p)
2+
// n -> number of courses, p -> the length of prerequisites
3+
// SC: O(n + m)
4+
// n -> the length of the graph size, m -> the length of nested list's size
5+
class Solution {
6+
public boolean canFinish(int numCourses, int[][] prerequisites) {
7+
List<List<Integer>> graph = new ArrayList<>();
8+
int[] inDegree = new int[numCourses];
9+
10+
for (int i = 0; i < numCourses; i++) graph.add(new ArrayList<>());
11+
12+
for (int[] prerequisite : prerequisites) {
13+
int course = prerequisite[0];
14+
int pre = prerequisite[1];
15+
graph.get(pre).add(course);
16+
inDegree[course] += 1;
17+
}
18+
19+
Queue<Integer> q = new LinkedList<>();
20+
for (int i = 0; i < numCourses; i++) {
21+
if (inDegree[i] == 0) q.offer(i);
22+
}
23+
24+
int visitedCourses = 0;
25+
while (!q.isEmpty()) {
26+
int course = q.poll();
27+
visitedCourses += 1;
28+
29+
for (int nextCourse : graph.get(course)) {
30+
inDegree[nextCourse] -= 1;
31+
if (inDegree[nextCourse] == 0) q.offer(nextCourse);
32+
}
33+
}
34+
35+
return visitedCourses == numCourses;
36+
}
37+
}

invert-binary-tree/TonyKim9401.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// TC: O(n)
2+
// -> visit all nodes to invert
3+
// SC: O(n)
4+
// -> create all nodes again to exchange
5+
class Solution {
6+
public TreeNode invertTree(TreeNode root) {
7+
if (root == null) return null;
8+
invertTree(root.left);
9+
invertTree(root.right);
10+
TreeNode left = root.left;
11+
TreeNode right = root.right;
12+
root.left = right;
13+
root.right = left;
14+
return root;
15+
}
16+
}

jump-game/TonyKim9401.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
class Solution {
4+
public boolean canJump(int[] nums) {
5+
int jump = 0;
6+
7+
for (int i = 0; i < nums.length; i++) {
8+
if (i > jump) return false;
9+
jump = Math.max(jump, i + nums[i]);
10+
}
11+
return true;
12+
}
13+
}

merge-k-sorted-lists/TonyKim9401.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// TC: O(n * log m)
2+
// m -> the number of the lists, n -> the number of node
3+
// SC: O(m)
4+
// m -> the number of the lists (max m)
5+
class Solution {
6+
public ListNode mergeKLists(ListNode[] lists) {
7+
if (lists == null || lists.length == 0) return null;
8+
9+
PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> a.val - b.val);
10+
11+
for (ListNode node : lists) {
12+
if (node != null) pq.offer(node);
13+
}
14+
15+
ListNode dummy = new ListNode(0);
16+
ListNode current = dummy;
17+
18+
while (!pq.isEmpty()) {
19+
ListNode minNode = pq.poll();
20+
current.next = minNode;
21+
current = current.next;
22+
23+
if (minNode.next != null) {
24+
pq.offer(minNode.next);
25+
}
26+
}
27+
28+
return dummy.next;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// TC: O(log n)
2+
// -> binary search
3+
// SC: O(1)
4+
class Solution {
5+
public int search(int[] nums, int target) {
6+
7+
int start = 0;
8+
int end = nums.length - 1;
9+
10+
while (start <= end) {
11+
int mid = start + (end - start) / 2;
12+
13+
if (nums[mid] == target) return mid;
14+
15+
if (nums[start] <= nums[mid]) {
16+
if (nums[start] <= target && target < nums[mid]) end = mid - 1;
17+
else start = mid + 1;
18+
} else {
19+
if (nums[mid] < target && target <= nums[end]) start = mid + 1;
20+
else end = mid - 1;
21+
}
22+
}
23+
return -1;
24+
}
25+
}

0 commit comments

Comments
 (0)