Skip to content

Commit 052a29e

Browse files
authored
Merge pull request #1595 from minji-go/week12
[minji-go] week 12-10 solutions
2 parents bc86994 + da93a18 commit 052a29e

File tree

13 files changed

+449
-14
lines changed

13 files changed

+449
-14
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/binary-tree-maximum-path-sum/">week11-5. binary-tree-maximum-path-sum</a>
3+
* <li>Description: Given the root of a binary tree, return the maximum path sum of any non-empty path</li>
4+
* <li>Topics: Dynamic Programming, Tree, Depth-First Search, Binary Tree</li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(H), Memory 44.1MB</li>
7+
*/
8+
class Solution {
9+
public int maxPathSum(TreeNode root) {
10+
dfs(root);
11+
return max;
12+
}
13+
14+
int max = Integer.MIN_VALUE;
15+
public int dfs(TreeNode head) {
16+
if (head == null) return 0;
17+
18+
int left = Math.max(0, dfs(head.left));
19+
int right = Math.max(0, dfs(head.right));
20+
21+
max = Math.max(max, head.val + left + right);
22+
23+
return head.val + Math.max(left, right);
24+
}
25+
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/course-schedule/">week10-3. course-schedule </a>
3+
* <li>Description: Return true if you can finish all courses. Otherwise, return false </li>
4+
* <li>Topics: Depth-First Search, Breadth-First Search, Graph, Topological Sort </li>
5+
* <li>Time Complexity: O(N+E), Runtime 7ms </li>
6+
* <li>Space Complexity: O(N+E), Memory 45.68MB </li>
7+
* <li>Note : refer to the answer. remember the topic of topological sort </li>
8+
*/
9+
class Solution {
10+
public boolean canFinish(int numCourses, int[][] prerequisites) {
11+
int[] inDegree = new int[numCourses];
12+
List<List<Integer>> graph = new ArrayList<>();
13+
for (int i=0; i<numCourses; i++){
14+
graph.add(new ArrayList<>());
15+
}
16+
17+
for(int[] pre : prerequisites) {
18+
int dest = pre[0], src = pre[1];
19+
graph.get(src).add(dest);
20+
inDegree[dest]++;
21+
}
22+
23+
Queue<Integer> queue = new LinkedList<>();
24+
for (int i=0; i<numCourses; i++) {
25+
if(inDegree[i] == 0) {
26+
queue.offer(i);
27+
}
28+
}
29+
30+
int count = 0;
31+
while(!queue.isEmpty()){
32+
int node = queue.poll();
33+
count++;
34+
35+
for(int neighbor : graph.get(node)) {
36+
inDegree[neighbor]--;
37+
if(inDegree[neighbor] == 0) {
38+
queue.offer(neighbor);
39+
}
40+
}
41+
}
42+
43+
return count == numCourses;
44+
}
45+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/invert-binary-tree/">week10-1. invert-binary-tree </a>
3+
* <li>Description: Design an algorithm to serialize and deserialize a binary tree </li>
4+
* <li>Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(N), Memory 41.28MB </li>
7+
*/
8+
class Solution {
9+
public TreeNode invertTree(TreeNode root) {
10+
if (root == null) return null;
11+
12+
Queue<TreeNode> queue = new LinkedList<>();
13+
queue.offer(root);
14+
15+
while (!queue.isEmpty()) {
16+
TreeNode parent = queue.poll();
17+
TreeNode temp = parent.left;
18+
parent.left = parent.right;
19+
parent.right = temp;
20+
if (parent.left != null) queue.offer(parent.left);
21+
if (parent.right != null) queue.offer(parent.right);
22+
}
23+
24+
return root;
25+
}
26+
}

‎jump-game/minji-go.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/jump-game/">week10-4. jump-game</a>
3+
* <li>Description: Return true if you can reach the last index, or false otherwise</li>
4+
* <li>Topics: Array, Dynamic Programming, Greedy </li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(1), Memory 45.66MB </li>
7+
*/
8+
class Solution {
9+
public boolean canJump(int[] nums) {
10+
int jump = 0;
11+
for (int i = 0; i <= jump; i++) {
12+
jump = Math.max(jump, i + nums[i]);
13+
if (jump >= nums.length - 1) {
14+
return true;
15+
}
16+
}
17+
return false;
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/merge-intervals/">week11-4. merge-intervals</a>
3+
* <li>Description: return an array of the non-overlapping intervals</li>
4+
* <li>Topics: Array, Sorting </li>
5+
* <li>Time Complexity: O(NlogN), Runtime 10ms </li>
6+
* <li>Space Complexity: O(N), Memory 46.18MB </li>
7+
*/
8+
class Solution {
9+
public int[][] merge(int[][] intervals) {
10+
Arrays.sort(intervals, (i1, i2) -> i1[0] - i2[0]);
11+
12+
List<int[]> merge = new ArrayList<>();
13+
merge.add(intervals[0]);
14+
15+
for (int i = 1; i < intervals.length; i++) {
16+
int[] prev = merge.get(merge.size() - 1);
17+
int[] curr = intervals[i];
18+
19+
if (curr[0] <= prev[1]) {
20+
prev[1] = Math.max(prev[1], curr[1]);
21+
} else {
22+
merge.add(curr);
23+
}
24+
}
25+
26+
return merge.toArray(new int[merge.size()][]);
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/https://leetcode.com/problems/merge-k-sorted-lists/">week10-5. merge-k-sorted-lists</a>
3+
* <li>Description: Merge all the linked-lists into one sorted linked-list </li>
4+
* <li>Topics: Linked List, Divide and Conquer, Heap (Priority Queue), Merge Sort </li>
5+
* <li>Time Complexity: O(NlogK), Runtime 1ms </li>
6+
* <li>Space Complexity: O(K), Memory 44.7MB </li>
7+
*/
8+
class Solution {
9+
public ListNode mergeKLists(ListNode[] lists) {
10+
if (lists == null || lists.length == 0) {
11+
return null;
12+
}
13+
return mergeKLists(lists, 0, lists.length - 1);
14+
}
15+
16+
private ListNode mergeKLists(ListNode[] lists, int start, int end) {
17+
if (start == end) {
18+
return lists[start];
19+
}
20+
21+
int mid = (start + end) / 2;
22+
ListNode node1 = mergeKLists(lists, start, mid);
23+
ListNode node2 = mergeKLists(lists, mid + 1, end);
24+
return mergeTwoLists(node1, node2);
25+
}
26+
27+
private ListNode mergeTwoLists(ListNode node1, ListNode node2) {
28+
ListNode dummy = new ListNode(-10_001);
29+
ListNode merge = dummy;
30+
31+
while (node1 != null && node2 != null) {
32+
if (node1.val < node2.val) {
33+
merge.next = node1;
34+
merge = merge.next;
35+
node1 = node1.next;
36+
} else {
37+
merge.next = node2;
38+
merge = merge.next;
39+
node2 = node2.next;
40+
}
41+
}
42+
43+
if (node1 != null && node2 == null) {
44+
merge.next = node1;
45+
} else {
46+
merge.next = node2;
47+
}
48+
49+
return dummy.next;
50+
}
51+
}
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
/*
2-
Problem: https://leetcode.com/problems/missing-number/
3-
Description: return the only number in the range that is missing from the array.
4-
Concept: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
5-
Time Complexity: O(N), Runtime 0ms
6-
Space Complexity: O(1), Memory 45.71MB
7-
*/
1+
/**
2+
* <a href="https://leetcode.com/problems/missing-number/">week11-1. missing-number</a>
3+
* <li>Description: Return the only number in the range that is missing from the array</li>
4+
* <li>Topics: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 45.67MB </li>
7+
*/
88
class Solution {
99
public int missingNumber(int[] nums) {
10-
int n = nums.length;
11-
int missing = n;
12-
13-
for(int i=0; i<n; i++){
14-
missing ^= i;
15-
missing ^= nums[i];
10+
int sum = (nums.length) * (nums.length + 1) / 2;
11+
for (int i = 0; i < nums.length; i++) {
12+
sum -= nums[i];
1613
}
17-
return missing;
14+
return sum;
1815
}
1916
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/non-overlapping-intervals/">week12-3. non-overlapping-intervals</a>
3+
* <li>Description: return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping</li>
4+
* <li>Topics: Array, Dynamic Programming, Greedy, Sorting </li>
5+
* <li>Time Complexity: O(N), Runtime 45ms </li>
6+
* <li>Space Complexity: O(1), Memory 73.45MB </li>
7+
* <li>Note : refer to the answer </li>
8+
*/
9+
class Solution {
10+
public int eraseOverlapIntervals(int[][] intervals) {
11+
Arrays.sort(intervals, (i1, i2) -> i1[1] - i2[1]);
12+
13+
int skip = 0;
14+
int end = Integer.MIN_VALUE;
15+
16+
for (int[] interval : intervals) {
17+
if (interval[0] < end) {
18+
skip++;
19+
} else {
20+
end = interval[1];
21+
}
22+
}
23+
24+
return skip;
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/">week12-2. remove-nth-node-from-end-of-list</a>
3+
* <li>Description: Given the head of a linked list, remove the nth node from the end of the list and return its head</li>
4+
* <li>Topics: Linked List, Two Pointers </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 41.95MB </li>
7+
*/
8+
class Solution {
9+
public ListNode removeNthFromEnd(ListNode head, int n) {
10+
ListNode dummy = new ListNode(0, head);
11+
ListNode fast = dummy;
12+
ListNode slow = dummy;
13+
14+
for(int i=0; i<=n; i++){
15+
fast = fast.next;
16+
}
17+
18+
while (fast != null) {
19+
slow = slow.next;
20+
fast = fast.next;
21+
}
22+
23+
slow.next = slow.next.next;
24+
25+
return dummy.next;
26+
}
27+
}

‎reorder-list/minji-go.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/reorder-list/">week11-2. reorder-list</a>
3+
* <li>Description: Reorder the list to be on the following form: L0 → Ln → L1 → Ln - 1 → L2 → ... </li>
4+
* <li>Topics: Linked List, Two Pointers, Stack, Recursion </li>
5+
* <li>Time Complexity: O(N), Runtime 5ms </li>
6+
* <li>Space Complexity: O(N), Memory 47.96MB </li>
7+
*/
8+
class Solution {
9+
public void reorderList(ListNode head) {
10+
Deque<ListNode> deque = new ArrayDeque<>();
11+
12+
ListNode node = head;
13+
while (node != null) {
14+
deque.addLast(node);
15+
node = node.next;
16+
}
17+
18+
ListNode curr = deque.removeFirst();
19+
while (!deque.isEmpty()) {
20+
curr.next = deque.removeLast();
21+
curr = curr.next;
22+
if (!deque.isEmpty()) {
23+
curr.next = deque.removeFirst();
24+
curr = curr.next;
25+
}
26+
}
27+
curr.next = null;
28+
}
29+
}

0 commit comments

Comments
 (0)