Skip to content

[sora0319] Week 10 Solutions #1559

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 6 commits into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions course-schedule/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] degree = new int[numCourses];

Map<Integer, List<Integer>> graph = new HashMap<>();

for (int[] p : prerequisites) {
int course = p[0];
int pre = p[1];

degree[course]++;

if (!graph.containsKey(pre)) {
graph.put(pre, new ArrayList<>());
}
graph.get(pre).add(course);
}

Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < numCourses; i++) {
if (degree[i] == 0) {
q.offer(i);
}
}

int finishedCourses = 0;

while (!q.isEmpty()) {
int curr = q.poll();
finishedCourses++;

if (graph.containsKey(curr)) {
for (int neighbor : graph.get(curr)) {
degree[neighbor]--;
if (degree[neighbor] == 0) {
q.offer(neighbor);
}
}
}
}

if(finishedCourses == numCourses) return true;

return false;
}
}

15 changes: 15 additions & 0 deletions invert-binary-tree/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}

TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);

root.left = right;
root.right = left;

return root;
}
}
11 changes: 11 additions & 0 deletions jump-game/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Solution {
public boolean canJump(int[] nums) {
int longLength = 0;
for (int i = 0; i < nums.length; i++) {
if (i > longLength) return false;
longLength = Math.max(longLength, i + nums[i]);
}
return true;
}
}

26 changes: 26 additions & 0 deletions merge-k-sorted-lists/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> pq = new PriorityQueue<>(Comparator.comparingInt(node -> node.val));

for (ListNode list : lists) {
if (list != null) {
pq.offer(list);
}
}

ListNode temp = new ListNode(-1);
ListNode head = temp;

while (!pq.isEmpty()) {
ListNode minNode = pq.poll();
head.next = minNode;
head = head.next;

if (minNode.next != null) {
pq.offer(minNode.next);
}
}

return temp.next;
}
}
51 changes: 51 additions & 0 deletions search-in-rotated-sorted-array/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
public class Solution {
public int search(int[] nums, int target) {
int p = findPivot(nums);

int index = binarySearch(nums, 0, p - 1, target);
if (index != -1) {
return index;
}
return binarySearch(nums, p, nums.length - 1, target);
}

private int findPivot(int[] nums) {
int l = 0;
int h = nums.length - 1;

while (l <= h) {
int mid = l + (h - l) / 2;

if (mid > 0 && nums[mid - 1] > nums[mid]) {
return mid;
}

if (nums[0] <= nums[mid]) {
l = mid + 1;
} else {
h = mid - 1;
}
}

return 0;
}

private int binarySearch(int[] nums, int l, int h, int target) {
while (l <= h) {
int mid = l + (h - l) / 2;

if (nums[mid] == target) {
return mid;
}

if (nums[mid] < target) {
l = mid + 1;
} else {
h = mid - 1;
}
}

return -1;
}
}