Skip to content
26 changes: 26 additions & 0 deletions binary-tree-maximum-path-sum/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* <a href="https://leetcode.com/problems/binary-tree-maximum-path-sum/">week11-5. binary-tree-maximum-path-sum</a>
* <li>Description: Given the root of a binary tree, return the maximum path sum of any non-empty path</li>
* <li>Topics: Dynamic Programming, Tree, Depth-First Search, Binary Tree</li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(H), Memory 44.1MB</li>
*/
class Solution {
public int maxPathSum(TreeNode root) {
dfs(root);
return max;
}

int max = Integer.MIN_VALUE;
public int dfs(TreeNode head) {
if (head == null) return 0;

int left = Math.max(0, dfs(head.left));
int right = Math.max(0, dfs(head.right));

max = Math.max(max, head.val + left + right);

return head.val + Math.max(left, right);
}

}
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;
}
}
28 changes: 28 additions & 0 deletions merge-intervals/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* <a href="https://leetcode.com/problems/merge-intervals/">week11-4. merge-intervals</a>
* <li>Description: return an array of the non-overlapping intervals</li>
* <li>Topics: Array, Sorting </li>
* <li>Time Complexity: O(NlogN), Runtime 10ms </li>
* <li>Space Complexity: O(N), Memory 46.18MB </li>
*/
class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (i1, i2) -> i1[0] - i2[0]);

List<int[]> merge = new ArrayList<>();
merge.add(intervals[0]);

for (int i = 1; i < intervals.length; i++) {
int[] prev = merge.get(merge.size() - 1);
int[] curr = intervals[i];

if (curr[0] <= prev[1]) {
prev[1] = Math.max(prev[1], curr[1]);
} else {
merge.add(curr);
}
}

return merge.toArray(new int[merge.size()][]);
}
}
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;
}
}
25 changes: 11 additions & 14 deletions missing-number/minji-go.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
/*
Problem: https://leetcode.com/problems/missing-number/
Description: return the only number in the range that is missing from the array.
Concept: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
Time Complexity: O(N), Runtime 0ms
Space Complexity: O(1), Memory 45.71MB
*/
/**
* <a href="https://leetcode.com/problems/missing-number/">week11-1. missing-number</a>
* <li>Description: Return the only number in the range that is missing from the array</li>
* <li>Topics: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting </li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(1), Memory 45.67MB </li>
*/
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int missing = n;

for(int i=0; i<n; i++){
missing ^= i;
missing ^= nums[i];
int sum = (nums.length) * (nums.length + 1) / 2;
for (int i = 0; i < nums.length; i++) {
sum -= nums[i];
}
return missing;
return sum;
}
}
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;
}
}
29 changes: 29 additions & 0 deletions reorder-list/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* <a href="https://leetcode.com/problems/reorder-list/">week11-2. reorder-list</a>
* <li>Description: Reorder the list to be on the following form: L0 → Ln → L1 → Ln - 1 → L2 → ... </li>
* <li>Topics: Linked List, Two Pointers, Stack, Recursion </li>
* <li>Time Complexity: O(N), Runtime 5ms </li>
* <li>Space Complexity: O(N), Memory 47.96MB </li>
*/
class Solution {
public void reorderList(ListNode head) {
Deque<ListNode> deque = new ArrayDeque<>();

ListNode node = head;
while (node != null) {
deque.addLast(node);
node = node.next;
}

ListNode curr = deque.removeFirst();
while (!deque.isEmpty()) {
curr.next = deque.removeLast();
curr = curr.next;
if (!deque.isEmpty()) {
curr.next = deque.removeFirst();
curr = curr.next;
}
}
curr.next = null;
}
}
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;
}
}
Loading