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

Added tasks 623, 628, 629, 630, 632 #244

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
javadev merged 3 commits into javadev:main from ThanhNIT:tasks-623-628-629-630-632
Jan 13, 2022
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
35 changes: 35 additions & 0 deletions src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g0601_0700.s0623_add_one_row_to_tree;

// #Medium #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree

import com_github_leetcode.TreeNode;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tags

ThanhNIT reacted with thumbs up emoji

public class Solution {
public TreeNode addOneRow(TreeNode root, int val, int depth) {
if (depth == 1) {
TreeNode newRoot = new TreeNode(val);
newRoot.left = root;
return newRoot;
}
dfs(root, depth - 2, val);
return root;
}

private void dfs(TreeNode node, int depth, int val) {
if (depth == 0) {
TreeNode left = new TreeNode(val);
TreeNode right = new TreeNode(val);
left.left = node.left;
right.right = node.right;
node.left = left;
node.right = right;
} else {
if (node.left != null) {
dfs(node.left, depth - 1, val);
}
if (node.right != null) {
dfs(node.right, depth - 1, val);
}
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/g0601_0700/s0623_add_one_row_to_tree/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
623\. Add One Row to Tree

Medium

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`.

Note that the `root` node is at depth `1`.

The adding rule is:

* 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.
* `cur`'s original left subtree should be the left subtree of the new left subtree root.
* `cur`'s original right subtree should be the right subtree of the new right subtree root.
* 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.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/03/15/addrow-tree.jpg)

**Input:** root = [4,2,6,3,1,5], val = 1, depth = 2

**Output:** [4,1,1,2,null,null,6,3,1,5]

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/03/11/add2-tree.jpg)

**Input:** root = [4,2,null,3,1], val = 1, depth = 3

**Output:** [4,2,null,1,1,3,null,null,1]

**Constraints:**

* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
* The depth of the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
* `-100 <= Node.val <= 100`
* <code>-10<sup>5</sup> <= val <= 10<sup>5</sup></code>
* `1 <= depth <= the depth of tree + 1`
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g0601_0700.s0628_maximum_product_of_three_numbers;

// #Easy #Array #Math #Sorting

public class Solution {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tags

ThanhNIT reacted with thumbs up emoji
public int maximumProduct(int[] a) {
int min1 = Integer.MAX_VALUE;
int min2 = Integer.MAX_VALUE;
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
int max3 = Integer.MIN_VALUE;
for (int i : a) {
if (i > max1) {
max3 = max2;
max2 = max1;
max1 = i;
} else if (i > max2) {
max3 = max2;
max2 = i;
} else if (i > max3) {
max3 = i;
}
if (i < min1) {
min2 = min1;
min1 = i;
} else if (i < min2) {
min2 = i;
}
}
return Math.max(min1 * min2 * max1, max1 * max2 * max3);
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
628\. Maximum Product of Three Numbers

Easy

Given an integer array `nums`, _find three numbers whose product is maximum and return the maximum product_.

**Example 1:**

**Input:** nums = [1,2,3]

**Output:** 6

**Example 2:**

**Input:** nums = [1,2,3,4]

**Output:** 24

**Example 3:**

**Input:** nums = [-1,-2,-3]

**Output:** -6

**Constraints:**

* <code>3 <= nums.length <= 10<sup>4</sup></code>
* `-1000 <= nums[i] <= 1000`
29 changes: 29 additions & 0 deletions src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g0601_0700.s0629_k_inverse_pairs_array;

// #Hard #Dynamic_Programming

public class Solution {
public int kinversepairs(int n, int k) {
k = Math.min(k, n * (n - 1) / 2 - k);
if (k < 0) {
return 0;
}
int[] dp = new int[k + 1];
int[] dp1 = new int[k + 1];
dp[0] = 1;
dp1[0] = 1;
int mod = 1_000_000_007;
for (int i = 1; i <= n; i++) {
int[] temp = dp;
dp = dp1;
dp1 = temp;
for (int j = 1, m = Math.min(k, i * (i - 1) / 2); j <= m; j++) {
dp[j] = (dp1[j] + dp[j - 1] - (j >= i ? dp1[j - i] : 0)) % mod;
if (dp[j] < 0) {
dp[j] += mod;
}
}
}
return dp[k];
}
}
28 changes: 28 additions & 0 deletions src/main/java/g0601_0700/s0629_k_inverse_pairs_array/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
629\. K Inverse Pairs Array

Hard

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]`.

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>.

**Example 1:**

**Input:** n = 3, k = 0

**Output:** 1

**Explanation:** Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.

**Example 2:**

**Input:** n = 3, k = 1

**Output:** 2

**Explanation:** The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.

**Constraints:**

* `1 <= n <= 1000`
* `0 <= k <= 1000`
93 changes: 93 additions & 0 deletions src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package g0601_0700.s0630_course_schedule_iii;

// #Hard #Array #Greedy #Heap_Priority_Queue

import java.util.Arrays;

public class Solution {
public int scheduleCourse(int[][] courses) {
Arrays.sort(courses, (a, b) -> a[1] - b[1]);
int course = 0;
int time = 0;
MaxHeap heap = new MaxHeap(courses.length);
for (int[] cours : courses) {
if (cours[1] - time >= cours[0]) {
time += cours[0];
course++;
heap.add(cours[0]);
} else if (cours[0] < heap.getHeap()[0]) {
int t = heap.pop();
heap.add(cours[0]);
time = time - t + cours[0];
}
}
return course;
}

static class MaxHeap {
private final int[] heap;
private int pin;

public MaxHeap(int mexLen) {
this.heap = new int[mexLen];
this.pin = 0;
}

public int[] getHeap() {
return heap;
}

public int getPin() {
return pin;
}

public void setPin(int pin) {
this.pin = pin;
}

public void add(int e) {
heap[pin] = e;
int temp = pin;
pin++;
while (temp > 0 && heap[(temp - 1) / 2] < e) {
heap[temp] = heap[(temp - 1) / 2];
temp = (temp - 1) / 2;
heap[temp] = e;
}
}

public int pop() {
int res = heap[0];
pin--;
heap[0] = heap[pin];
int h0 = heap[0];
int temp = 0;
while (temp * 2 + 1 < pin) {
if (temp * 2 + 2 == pin) {
if (heap[temp * 2 + 1] > h0) {
heap[temp] = heap[temp * 2 + 1];
temp = temp * 2 + 1;
heap[temp] = h0;
} else {
break;
}
} else {
if (h0 < heap[temp * 2 + 1] || h0 < heap[temp * 2 + 2]) {
if (heap[temp * 2 + 1] > heap[temp * 2 + 2]) {
heap[temp] = heap[temp * 2 + 1];
temp = temp * 2 + 1;
heap[temp] = h0;
} else {
heap[temp] = heap[temp * 2 + 2];
temp = temp * 2 + 2;
heap[temp] = h0;
}
} else {
break;
}
}
}
return res;
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/g0601_0700/s0630_course_schedule_iii/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
630\. Course Schedule III

Hard

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>.

You will start on the <code>1<sup>st</sup></code> day and you cannot take two or more courses simultaneously.

Return _the maximum number of courses that you can take_.

**Example 1:**

**Input:** courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]

**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.

**Example 2:**

**Input:** courses = [[1,2]]

**Output:** 1

**Example 3:**

**Input:** courses = [[3,2],[4,3]]

**Output:** 0

**Constraints:**

* <code>1 <= courses.length <= 10<sup>4</sup></code>
* <code>1 <= duration<sub>i</sub>, lastDay<sub>i</sub> <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package g0601_0700.s0632_smallest_range_covering_elements_from_k_lists;

// #Hard #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Sliding_Window

import java.util.List;
import java.util.Objects;
import java.util.PriorityQueue;

public class Solution {
static class Triplet implements Comparable<Triplet> {
int value;
int row;
int idx;

Triplet(int value, int row, int idx) {
this.value = value;
this.row = row;
this.idx = idx;
}

public int compareTo(Triplet obj) {
return this.value - obj.value;
}
}

public int[] smallestRange(List<List<Integer>> nums) {
PriorityQueue<Triplet> pq = new PriorityQueue<>();
int maxInPq = Integer.MIN_VALUE;
for (int i = 0; i < nums.size(); i++) {
pq.add(new Triplet(nums.get(i).get(0), i, 0));
if (maxInPq < nums.get(i).get(0)) {
maxInPq = nums.get(i).get(0);
}
}
int rangeSize = maxInPq - Objects.requireNonNull(pq.peek()).value + 1;
int rangeLeft = Objects.requireNonNull(pq.peek()).value;
int rangeRight = maxInPq;
while (true) {
Triplet nextNumber = pq.remove();
if (nextNumber.idx + 1 < nums.get(nextNumber.row).size()) {
int val = nums.get(nextNumber.row).get(nextNumber.idx + 1);
if (val > maxInPq) {
maxInPq = val;
}
pq.add(new Triplet(val, nextNumber.row, nextNumber.idx + 1));
if (maxInPq - Objects.requireNonNull(pq.peek()).value + 1 < rangeSize) {
rangeSize = maxInPq - pq.peek().value + 1;
rangeLeft = maxInPq;
rangeRight = pq.peek().value;
}
} else {
break;
}
}
int[] answer = new int[2];
answer[0] = rangeLeft;
answer[1] = rangeRight;
return answer;
}
}
Loading

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