Skip to content

Commit 5880bd8

Browse files
authored
Merge pull request #1559 from sora0319/main
[sora0319] Week 10 Solutions
2 parents a281b90 + 0ebd4a5 commit 5880bd8

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

course-schedule/sora0319.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Solution {
2+
public boolean canFinish(int numCourses, int[][] prerequisites) {
3+
int[] degree = new int[numCourses];
4+
5+
Map<Integer, List<Integer>> graph = new HashMap<>();
6+
7+
for (int[] p : prerequisites) {
8+
int course = p[0];
9+
int pre = p[1];
10+
11+
degree[course]++;
12+
13+
if (!graph.containsKey(pre)) {
14+
graph.put(pre, new ArrayList<>());
15+
}
16+
graph.get(pre).add(course);
17+
}
18+
19+
Queue<Integer> q = new LinkedList<>();
20+
for (int i = 0; i < numCourses; i++) {
21+
if (degree[i] == 0) {
22+
q.offer(i);
23+
}
24+
}
25+
26+
int finishedCourses = 0;
27+
28+
while (!q.isEmpty()) {
29+
int curr = q.poll();
30+
finishedCourses++;
31+
32+
if (graph.containsKey(curr)) {
33+
for (int neighbor : graph.get(curr)) {
34+
degree[neighbor]--;
35+
if (degree[neighbor] == 0) {
36+
q.offer(neighbor);
37+
}
38+
}
39+
}
40+
}
41+
42+
if(finishedCourses == numCourses) return true;
43+
44+
return false;
45+
}
46+
}
47+

invert-binary-tree/sora0319.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public TreeNode invertTree(TreeNode root) {
3+
if (root == null) {
4+
return null;
5+
}
6+
7+
TreeNode left = invertTree(root.left);
8+
TreeNode right = invertTree(root.right);
9+
10+
root.left = right;
11+
root.right = left;
12+
13+
return root;
14+
}
15+
}

jump-game/sora0319.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Solution {
2+
public boolean canJump(int[] nums) {
3+
int longLength = 0;
4+
for (int i = 0; i < nums.length; i++) {
5+
if (i > longLength) return false;
6+
longLength = Math.max(longLength, i + nums[i]);
7+
}
8+
return true;
9+
}
10+
}
11+

merge-k-sorted-lists/sora0319.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public ListNode mergeKLists(ListNode[] lists) {
3+
PriorityQueue<ListNode> pq = new PriorityQueue<>(Comparator.comparingInt(node -> node.val));
4+
5+
for (ListNode list : lists) {
6+
if (list != null) {
7+
pq.offer(list);
8+
}
9+
}
10+
11+
ListNode temp = new ListNode(-1);
12+
ListNode head = temp;
13+
14+
while (!pq.isEmpty()) {
15+
ListNode minNode = pq.poll();
16+
head.next = minNode;
17+
head = head.next;
18+
19+
if (minNode.next != null) {
20+
pq.offer(minNode.next);
21+
}
22+
}
23+
24+
return temp.next;
25+
}
26+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
public class Solution {
2+
public int search(int[] nums, int target) {
3+
int p = findPivot(nums);
4+
5+
int index = binarySearch(nums, 0, p - 1, target);
6+
if (index != -1) {
7+
return index;
8+
}
9+
return binarySearch(nums, p, nums.length - 1, target);
10+
}
11+
12+
private int findPivot(int[] nums) {
13+
int l = 0;
14+
int h = nums.length - 1;
15+
16+
while (l <= h) {
17+
int mid = l + (h - l) / 2;
18+
19+
if (mid > 0 && nums[mid - 1] > nums[mid]) {
20+
return mid;
21+
}
22+
23+
if (nums[0] <= nums[mid]) {
24+
l = mid + 1;
25+
} else {
26+
h = mid - 1;
27+
}
28+
}
29+
30+
return 0;
31+
}
32+
33+
private int binarySearch(int[] nums, int l, int h, int target) {
34+
while (l <= h) {
35+
int mid = l + (h - l) / 2;
36+
37+
if (nums[mid] == target) {
38+
return mid;
39+
}
40+
41+
if (nums[mid] < target) {
42+
l = mid + 1;
43+
} else {
44+
h = mid - 1;
45+
}
46+
}
47+
48+
return -1;
49+
}
50+
}
51+

0 commit comments

Comments
 (0)