Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cbad294

Browse files
Added tasks 623, 628, 629, 630, 632.
1 parent a45de88 commit cbad294

File tree

15 files changed

+528
-0
lines changed

15 files changed

+528
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0601_0700.s0623_add_one_row_to_tree;
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
5+
import com_github_leetcode.TreeNode;
6+
7+
public class Solution {
8+
public TreeNode addOneRow(TreeNode root, int val, int depth) {
9+
if (depth == 1) {
10+
TreeNode newRoot = new TreeNode(val);
11+
newRoot.left = root;
12+
return newRoot;
13+
}
14+
dfs(root, depth - 2, val);
15+
return root;
16+
}
17+
18+
private void dfs(TreeNode node, int depth, int val) {
19+
if (depth == 0) {
20+
TreeNode left = new TreeNode(val);
21+
TreeNode right = new TreeNode(val);
22+
left.left = node.left;
23+
right.right = node.right;
24+
node.left = left;
25+
node.right = right;
26+
} else {
27+
if (node.left != null) {
28+
dfs(node.left, depth - 1, val);
29+
}
30+
if (node.right != null) {
31+
dfs(node.right, depth - 1, val);
32+
}
33+
}
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
623\. Add One Row to Tree
2+
3+
Medium
4+
5+
Given the `root` of a binary tree and two integers `val` and `depth`, add a row of nodes with value `val` at the given depth `depth`.
6+
7+
Note that the `root` node is at depth `1`.
8+
9+
The adding rule is:
10+
11+
* Given the integer `depth`, for each not null tree node `cur` at the depth `depth - 1`, create two tree nodes with value `val` as `cur`'s left subtree root and right subtree root.
12+
* `cur`'s original left subtree should be the left subtree of the new left subtree root.
13+
* `cur`'s original right subtree should be the right subtree of the new right subtree root.
14+
* If `depth == 1` that means there is no depth `depth - 1` at all, then create a tree node with value `val` as the new root of the whole original tree, and the original tree is the new root's left subtree.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2021/03/15/addrow-tree.jpg)
19+
20+
**Input:** root = [4,2,6,3,1,5], val = 1, depth = 2
21+
22+
**Output:** [4,1,1,2,null,null,6,3,1,5]
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2021/03/11/add2-tree.jpg)
27+
28+
**Input:** root = [4,2,null,3,1], val = 1, depth = 3
29+
30+
**Output:** [4,2,null,1,1,3,null,null,1]
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
35+
* The depth of the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
36+
* `-100 <= Node.val <= 100`
37+
* <code>-10<sup>5</sup> <= val <= 10<sup>5</sup></code>
38+
* `1 <= depth <= the depth of tree + 1`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0601_0700.s0628_maximum_product_of_three_numbers;
2+
3+
// #Easy #Array #Math #Sorting
4+
5+
public class Solution {
6+
public int maximumProduct(int[] a) {
7+
int min1 = Integer.MAX_VALUE;
8+
int min2 = Integer.MAX_VALUE;
9+
int max1 = Integer.MIN_VALUE;
10+
int max2 = Integer.MIN_VALUE;
11+
int max3 = Integer.MIN_VALUE;
12+
for (int i : a) {
13+
if (i > max1) {
14+
max3 = max2;
15+
max2 = max1;
16+
max1 = i;
17+
} else if (i > max2) {
18+
max3 = max2;
19+
max2 = i;
20+
} else if (i > max3) {
21+
max3 = i;
22+
}
23+
if (i < min1) {
24+
min2 = min1;
25+
min1 = i;
26+
} else if (i < min2) {
27+
min2 = i;
28+
}
29+
}
30+
return Math.max(min1 * min2 * max1, max1 * max2 * max3);
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
628\. Maximum Product of Three Numbers
2+
3+
Easy
4+
5+
Given an integer array `nums`, _find three numbers whose product is maximum and return the maximum product_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3]
10+
11+
**Output:** 6
12+
13+
**Example 2:**
14+
15+
**Input:** nums = [1,2,3,4]
16+
17+
**Output:** 24
18+
19+
**Example 3:**
20+
21+
**Input:** nums = [-1,-2,-3]
22+
23+
**Output:** -6
24+
25+
**Constraints:**
26+
27+
* <code>3 <= nums.length <= 10<sup>4</sup></code>
28+
* `-1000 <= nums[i] <= 1000`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0601_0700.s0629_k_inverse_pairs_array;
2+
3+
// #Hard #Dynamic_Programming
4+
5+
public class Solution {
6+
public int kinversepairs(int n, int k) {
7+
k = Math.min(k, n * (n - 1) / 2 - k);
8+
if (k < 0) {
9+
return 0;
10+
}
11+
int[] dp = new int[k + 1];
12+
int[] dp1 = new int[k + 1];
13+
dp[0] = 1;
14+
dp1[0] = 1;
15+
int mod = 1_000_000_007;
16+
for (int i = 1; i <= n; i++) {
17+
int[] temp = dp;
18+
dp = dp1;
19+
dp1 = temp;
20+
for (int j = 1, m = Math.min(k, i * (i - 1) / 2); j <= m; j++) {
21+
dp[j] = (dp1[j] + dp[j - 1] - (j >= i ? dp1[j - i] : 0)) % mod;
22+
if (dp[j] < 0) {
23+
dp[j] += mod;
24+
}
25+
}
26+
}
27+
return dp[k];
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
629\. K Inverse Pairs Array
2+
3+
Hard
4+
5+
For an integer array `nums`, an **inverse pair** is a pair of integers `[i, j]` where `0 <= i < j < nums.length` and `nums[i] > nums[j]`.
6+
7+
Given two integers n and k, return the number of different arrays consist of numbers from `1` to `n` such that there are exactly `k` **inverse pairs**. Since the answer can be huge, return it **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 3, k = 0
12+
13+
**Output:** 1
14+
15+
**Explanation:** Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
16+
17+
**Example 2:**
18+
19+
**Input:** n = 3, k = 1
20+
21+
**Output:** 2
22+
23+
**Explanation:** The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
24+
25+
**Constraints:**
26+
27+
* `1 <= n <= 1000`
28+
* `0 <= k <= 1000`
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package g0601_0700.s0630_course_schedule_iii;
2+
3+
// #Hard #Array #Greedy #Heap_Priority_Queue
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int scheduleCourse(int[][] courses) {
9+
Arrays.sort(courses, (a, b) -> a[1] - b[1]);
10+
int course = 0;
11+
int time = 0;
12+
MaxHeap heap = new MaxHeap(courses.length);
13+
for (int[] cours : courses) {
14+
if (cours[1] - time >= cours[0]) {
15+
time += cours[0];
16+
course++;
17+
heap.add(cours[0]);
18+
} else if (cours[0] < heap.getHeap()[0]) {
19+
int t = heap.pop();
20+
heap.add(cours[0]);
21+
time = time - t + cours[0];
22+
}
23+
}
24+
return course;
25+
}
26+
27+
static class MaxHeap {
28+
private final int[] heap;
29+
private int pin;
30+
31+
public MaxHeap(int mexLen) {
32+
this.heap = new int[mexLen];
33+
this.pin = 0;
34+
}
35+
36+
public int[] getHeap() {
37+
return heap;
38+
}
39+
40+
public int getPin() {
41+
return pin;
42+
}
43+
44+
public void setPin(int pin) {
45+
this.pin = pin;
46+
}
47+
48+
public void add(int e) {
49+
heap[pin] = e;
50+
int temp = pin;
51+
pin++;
52+
while (temp > 0 && heap[(temp - 1) / 2] < e) {
53+
heap[temp] = heap[(temp - 1) / 2];
54+
temp = (temp - 1) / 2;
55+
heap[temp] = e;
56+
}
57+
}
58+
59+
public int pop() {
60+
int res = heap[0];
61+
pin--;
62+
heap[0] = heap[pin];
63+
int h0 = heap[0];
64+
int temp = 0;
65+
while (temp * 2 + 1 < pin) {
66+
if (temp * 2 + 2 == pin) {
67+
if (heap[temp * 2 + 1] > h0) {
68+
heap[temp] = heap[temp * 2 + 1];
69+
temp = temp * 2 + 1;
70+
heap[temp] = h0;
71+
} else {
72+
break;
73+
}
74+
} else {
75+
if (h0 < heap[temp * 2 + 1] || h0 < heap[temp * 2 + 2]) {
76+
if (heap[temp * 2 + 1] > heap[temp * 2 + 2]) {
77+
heap[temp] = heap[temp * 2 + 1];
78+
temp = temp * 2 + 1;
79+
heap[temp] = h0;
80+
} else {
81+
heap[temp] = heap[temp * 2 + 2];
82+
temp = temp * 2 + 2;
83+
heap[temp] = h0;
84+
}
85+
} else {
86+
break;
87+
}
88+
}
89+
}
90+
return res;
91+
}
92+
}
93+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
630\. Course Schedule III
2+
3+
Hard
4+
5+
There are `n` different online courses numbered from `1` to `n`. You are given an array `courses` where <code>courses[i] = [duration<sub>i</sub>, lastDay<sub>i</sub>]</code> indicate that the <code>i<sup>th</sup></code> course should be taken **continuously** for <code>duration<sub>i</sub></code> days and must be finished before or on <code>lastDay<sub>i</sub></code>.
6+
7+
You will start on the <code>1<sup>st</sup></code> day and you cannot take two or more courses simultaneously.
8+
9+
Return _the maximum number of courses that you can take_.
10+
11+
**Example 1:**
12+
13+
**Input:** courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
14+
15+
**Output:** 3 Explanation: There are totally 4 courses, but you can take 3 courses at most: First, take the 1<sup>st</sup> course, it costs 100 days so you will finish it on the 100<sup>th</sup> day, and ready to take the next course on the 101<sup>st</sup> day. Second, take the 3<sup>rd</sup> course, it costs 1000 days so you will finish it on the 1100<sup>th</sup> day, and ready to take the next course on the 1101<sup>st</sup> day. Third, take the 2<sup>nd</sup> course, it costs 200 days so you will finish it on the 1300<sup>th</sup> day. The 4<sup>th</sup> course cannot be taken now, since you will finish it on the 3300<sup>th</sup> day, which exceeds the closed date.
16+
17+
**Example 2:**
18+
19+
**Input:** courses = [[1,2]]
20+
21+
**Output:** 1
22+
23+
**Example 3:**
24+
25+
**Input:** courses = [[3,2],[4,3]]
26+
27+
**Output:** 0
28+
29+
**Constraints:**
30+
31+
* <code>1 <= courses.length <= 10<sup>4</sup></code>
32+
* <code>1 <= duration<sub>i</sub>, lastDay<sub>i</sub> <= 10<sup>4</sup></code>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g0601_0700.s0632_smallest_range_covering_elements_from_k_lists;
2+
3+
// #Hard #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Sliding_Window
4+
5+
import java.util.List;
6+
import java.util.Objects;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
static class Triplet implements Comparable<Triplet> {
11+
int value;
12+
int row;
13+
int idx;
14+
15+
Triplet(int value, int row, int idx) {
16+
this.value = value;
17+
this.row = row;
18+
this.idx = idx;
19+
}
20+
21+
public int compareTo(Triplet obj) {
22+
return this.value - obj.value;
23+
}
24+
}
25+
26+
public int[] smallestRange(List<List<Integer>> nums) {
27+
PriorityQueue<Triplet> pq = new PriorityQueue<>();
28+
int maxInPq = Integer.MIN_VALUE;
29+
for (int i = 0; i < nums.size(); i++) {
30+
pq.add(new Triplet(nums.get(i).get(0), i, 0));
31+
if (maxInPq < nums.get(i).get(0)) {
32+
maxInPq = nums.get(i).get(0);
33+
}
34+
}
35+
int rangeSize = maxInPq - Objects.requireNonNull(pq.peek()).value + 1;
36+
int rangeLeft = Objects.requireNonNull(pq.peek()).value;
37+
int rangeRight = maxInPq;
38+
while (true) {
39+
Triplet nextNumber = pq.remove();
40+
if (nextNumber.idx + 1 < nums.get(nextNumber.row).size()) {
41+
int val = nums.get(nextNumber.row).get(nextNumber.idx + 1);
42+
if (val > maxInPq) {
43+
maxInPq = val;
44+
}
45+
pq.add(new Triplet(val, nextNumber.row, nextNumber.idx + 1));
46+
if (maxInPq - Objects.requireNonNull(pq.peek()).value + 1 < rangeSize) {
47+
rangeSize = maxInPq - pq.peek().value + 1;
48+
rangeLeft = maxInPq;
49+
rangeRight = pq.peek().value;
50+
}
51+
} else {
52+
break;
53+
}
54+
}
55+
int[] answer = new int[2];
56+
answer[0] = rangeLeft;
57+
answer[1] = rangeRight;
58+
return answer;
59+
}
60+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /