Skip to content
45 changes: 45 additions & 0 deletions course-schedule/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* <a href="https://leetcode.com/problems/course-schedule/">week10-3. course-schedule </a>
* <li>Description: Return true if you can finish all courses. Otherwise, return false </li>
* <li>Topics: Depth-First Search, Breadth-First Search, Graph, Topological Sort </li>
* <li>Time Complexity: O(N+E), Runtime 7ms </li>
* <li>Space Complexity: O(N+E), Memory 45.68MB </li>
* <li>Note : refer to the answer. remember the topic of topological sort </li>
*/
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] inDegree = new int[numCourses];
List<List<Integer>> graph = new ArrayList<>();
for (int i=0; i<numCourses; i++){
graph.add(new ArrayList<>());
}

for(int[] pre : prerequisites) {
int dest = pre[0], src = pre[1];
graph.get(src).add(dest);
inDegree[dest]++;
}

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

int count = 0;
while(!queue.isEmpty()){
int node = queue.poll();
count++;

for(int neighbor : graph.get(node)) {
inDegree[neighbor]--;
if(inDegree[neighbor] == 0) {
queue.offer(neighbor);
}
}
}

return count == numCourses;
}
}
26 changes: 26 additions & 0 deletions invert-binary-tree/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* <a href="https://leetcode.com/problems/invert-binary-tree/">week10-1. invert-binary-tree </a>
* <li>Description: Design an algorithm to serialize and deserialize a binary tree </li>
* <li>Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree </li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(N), Memory 41.28MB </li>
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) return null;

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);

while (!queue.isEmpty()) {
TreeNode parent = queue.poll();
TreeNode temp = parent.left;
parent.left = parent.right;
parent.right = temp;
if (parent.left != null) queue.offer(parent.left);
if (parent.right != null) queue.offer(parent.right);
}

return root;
}
}
19 changes: 19 additions & 0 deletions jump-game/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* <a href="https://leetcode.com/problems/jump-game/">week10-4. jump-game</a>
* <li>Description: Return true if you can reach the last index, or false otherwise</li>
* <li>Topics: Array, Dynamic Programming, Greedy </li>
* <li>Time Complexity: O(N), Runtime 2ms </li>
* <li>Space Complexity: O(1), Memory 45.66MB </li>
*/
class Solution {
public boolean canJump(int[] nums) {
int jump = 0;
for (int i = 0; i <= jump; i++) {
jump = Math.max(jump, i + nums[i]);
if (jump >= nums.length - 1) {
return true;
}
}
return false;
}
}
51 changes: 51 additions & 0 deletions merge-k-sorted-lists/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* <a href="https://leetcode.com/problems/https://leetcode.com/problems/merge-k-sorted-lists/">week10-5. merge-k-sorted-lists</a>
* <li>Description: Merge all the linked-lists into one sorted linked-list </li>
* <li>Topics: Linked List, Divide and Conquer, Heap (Priority Queue), Merge Sort </li>
* <li>Time Complexity: O(NlogK), Runtime 1ms </li>
* <li>Space Complexity: O(K), Memory 44.7MB </li>
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
return mergeKLists(lists, 0, lists.length - 1);
}

private ListNode mergeKLists(ListNode[] lists, int start, int end) {
if (start == end) {
return lists[start];
}

int mid = (start + end) / 2;
ListNode node1 = mergeKLists(lists, start, mid);
ListNode node2 = mergeKLists(lists, mid + 1, end);
return mergeTwoLists(node1, node2);
}

private ListNode mergeTwoLists(ListNode node1, ListNode node2) {
ListNode dummy = new ListNode(-10_001);
ListNode merge = dummy;

while (node1 != null && node2 != null) {
if (node1.val < node2.val) {
merge.next = node1;
merge = merge.next;
node1 = node1.next;
} else {
merge.next = node2;
merge = merge.next;
node2 = node2.next;
}
}

if (node1 != null && node2 == null) {
merge.next = node1;
} else {
merge.next = node2;
}

return dummy.next;
}
}
26 changes: 26 additions & 0 deletions non-overlapping-intervals/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* <a href="https://leetcode.com/problems/non-overlapping-intervals/">week12-3. non-overlapping-intervals</a>
* <li>Description: return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping</li>
* <li>Topics: Array, Dynamic Programming, Greedy, Sorting </li>
* <li>Time Complexity: O(N), Runtime 45ms </li>
* <li>Space Complexity: O(1), Memory 73.45MB </li>
* <li>Note : refer to the answer </li>
*/
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (i1, i2) -> i1[1] - i2[1]);

int skip = 0;
int end = Integer.MIN_VALUE;

for (int[] interval : intervals) {
if (interval[0] < end) {
skip++;
} else {
end = interval[1];
}
}

return skip;
}
}
27 changes: 27 additions & 0 deletions remove-nth-node-from-end-of-list/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* <a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/">week12-2. remove-nth-node-from-end-of-list</a>
* <li>Description: Given the head of a linked list, remove the nth node from the end of the list and return its head</li>
* <li>Topics: Linked List, Two Pointers </li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(1), Memory 41.95MB </li>
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = dummy;
ListNode slow = dummy;

for(int i=0; i<=n; i++){
fast = fast.next;
}

while (fast != null) {
slow = slow.next;
fast = fast.next;
}

slow.next = slow.next.next;

return dummy.next;
}
}
36 changes: 36 additions & 0 deletions same-tree/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import leetcode_study.TreeNode;

import java.util.AbstractMap;
import java.util.Map;

/**
* <a href="https://leetcode.com/problems/same-tree/description/">week12-1. same-tree</a>
* <li>Description: Given the roots of two binary trees p and q, check if they are the same or not </li>
* <li>Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree </li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(N), Memory 41.14MB </li>
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
Queue<Map.Entry<TreeNode, TreeNode>> queue = new LinkedList<>();
queue.add(new AbstractMap.SimpleEntry<>(p, q));

while (!queue.isEmpty()) {
Map.Entry<TreeNode, TreeNode> nodeMap = queue.poll();
TreeNode nodeP = nodeMap.getKey();
TreeNode nodeQ = nodeMap.getValue();

if (nodeP == null && nodeQ == null) {
continue;
}
if (nodeP == null || nodeQ == null || nodeP.val != nodeQ.val) {
return false;
}

queue.add(new AbstractMap.SimpleEntry<>(nodeP.left, nodeQ.left));
queue.add(new AbstractMap.SimpleEntry<>(nodeP.right, nodeQ.right));
}

return true;
}
}
60 changes: 60 additions & 0 deletions search-in-rotated-sorted-array/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* <a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">week10-2. search-in-rotated-sorted-array</a>
* <li>Description: nums is sorted in ascending order and is possibly rotated at an unknown pivot index. return the index of target if it is in nums, or -1 </li>
* <li>Topics: Array, Binary Search </li>
* <li>Time Complexity: O(logN), Runtime 1ms </li>
* <li>Space Complexity: O(logN), Memory 43.23MB </li>
*/
class Solution {
public int search(int[] nums, int target) {
if(nums.length==1) {
if(nums[0]==target) return 0;
return -1;
}

Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{0, nums.length-1});

int pivot = -1;
if(nums[0]<nums[nums.length-1]) {
pivot=nums.length-1;
}

while(pivot==-1 && !queue.isEmpty()){
int[] q = queue.poll();
int mid = (q[0]+q[1])/2;
if(mid+1 > nums.length-1) {
continue;
}
if(nums[mid]>nums[mid+1]) {
pivot=mid;
break;
}
queue.add(new int[]{q[0], mid});
queue.add(new int[]{mid+1, q[1]});
}

int left = 0, right = nums.length-1;
if(target>=nums[0] && target<=nums[pivot]) {
right = pivot;
} else {
left = pivot+1;
}


while(left<=right){
int mid = (left+right)/2;
if(target < nums[mid]) {
right=mid-1;
} else {
left=mid+1;
}
}

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

return -1;
}
}
65 changes: 65 additions & 0 deletions serialize-and-deserialize-binary-tree/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* <a href="https://leetcode.com/problems/serialize-and-deserialize-binary-tree/">week12-4. serialize-and-deserialize-binary-tree </a>
* <li>Description: Design an algorithm to serialize and deserialize a binary tree </li>
* <li>Topics: String, Tree, Depth-First Search, Breadth-First Search, Design, Binary Tree </li>
* <li>Time Complexity: O(N), Runtime 14ms </li>
* <li>Space Complexity: O(N), Memory 45.44MB </li>
*/
public class Codec {

public String serialize(TreeNode root) {
if (root == null) {
return null;
}

Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

StringBuilder sb = new StringBuilder();
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node == null) {
sb.append("null,");
continue;
}
sb.append(node.val).append(",");
queue.add(node.left);
queue.add(node.right);
}

return sb.toString();
}

public TreeNode deserialize(String data) {
if (data == null) {
return null;
}

String[] values = data.split(",");

TreeNode root = new TreeNode(Integer.parseInt(values[0]));
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

int i = 1;
while (!queue.isEmpty()) {
TreeNode parent = queue.poll();

if (!values[i].equals("null")) {
TreeNode left = new TreeNode(Integer.parseInt(values[i]));
parent.left = left;
queue.add(left);
}
i++;

if (!values[i].equals("null")) {
TreeNode right = new TreeNode(Integer.parseInt(values[i]));
parent.right = right;
queue.add(right);
}
i++;
}

return root;
}
}