From c485f4e5c8507ab6cfefb640b1b7af357dc7e39e Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: 2022年1月13日 08:35:42 +0700 Subject: [PATCH 1/3] Added tasks 623, 628, 629, 630, 632 --- .../s0623_add_one_row_to_tree/Solution.java | 33 +++++++ .../s0623_add_one_row_to_tree/readme.md | 38 ++++++++ .../Solution.java | 30 ++++++ .../readme.md | 28 ++++++ .../s0629_k_inverse_pairs_array/Solution.java | 27 ++++++ .../s0629_k_inverse_pairs_array/readme.md | 28 ++++++ .../s0630_course_schedule_iii/Solution.java | 93 +++++++++++++++++++ .../s0630_course_schedule_iii/readme.md | 32 +++++++ .../Solution.java | 60 ++++++++++++ .../readme.md | 29 ++++++ .../SolutionTest.java | 26 ++++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 18 ++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 33 +++++++ 15 files changed, 522 insertions(+) create mode 100644 src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java create mode 100644 src/main/java/g0601_0700/s0623_add_one_row_to_tree/readme.md create mode 100644 src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.java create mode 100644 src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/readme.md create mode 100644 src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java create mode 100644 src/main/java/g0601_0700/s0629_k_inverse_pairs_array/readme.md create mode 100644 src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java create mode 100644 src/main/java/g0601_0700/s0630_course_schedule_iii/readme.md create mode 100644 src/main/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/Solution.java create mode 100644 src/main/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/readme.md create mode 100644 src/test/java/g0601_0700/s0623_add_one_row_to_tree/SolutionTest.java create mode 100644 src/test/java/g0601_0700/s0628_maximum_product_of_three_numbers/SolutionTest.java create mode 100644 src/test/java/g0601_0700/s0629_k_inverse_pairs_array/SolutionTest.java create mode 100644 src/test/java/g0601_0700/s0630_course_schedule_iii/SolutionTest.java create mode 100644 src/test/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/SolutionTest.java diff --git a/src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java b/src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java new file mode 100644 index 000000000..648752f10 --- /dev/null +++ b/src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java @@ -0,0 +1,33 @@ +package g0601_0700.s0623_add_one_row_to_tree; + +import com_github_leetcode.TreeNode; + +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); + } + } + } +} diff --git a/src/main/java/g0601_0700/s0623_add_one_row_to_tree/readme.md b/src/main/java/g0601_0700/s0623_add_one_row_to_tree/readme.md new file mode 100644 index 000000000..12d992f8a --- /dev/null +++ b/src/main/java/g0601_0700/s0623_add_one_row_to_tree/readme.md @@ -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 [1, 104]. +* The depth of the tree is in the range [1, 104]. +* `-100 <= Node.val <= 100` +* -105 <= val <= 105 +* `1 <= depth <= the depth of tree + 1` \ No newline at end of file diff --git a/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.java b/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.java new file mode 100644 index 000000000..f5dad409c --- /dev/null +++ b/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.java @@ -0,0 +1,30 @@ +package g0601_0700.s0628_maximum_product_of_three_numbers; + +public class Solution { + 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); + } +} diff --git a/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/readme.md b/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/readme.md new file mode 100644 index 000000000..7aed15e8e --- /dev/null +++ b/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/readme.md @@ -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:** + +* 3 <= nums.length <= 104 +* `-1000 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java b/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java new file mode 100644 index 000000000..fa1f8eaac --- /dev/null +++ b/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java @@ -0,0 +1,27 @@ +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]; + } +} diff --git a/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/readme.md b/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/readme.md new file mode 100644 index 000000000..1c8a88213 --- /dev/null +++ b/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/readme.md @@ -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** 109 + 7. + +**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` \ No newline at end of file diff --git a/src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java b/src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java new file mode 100644 index 000000000..d099930f3 --- /dev/null +++ b/src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java @@ -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; + } +} + +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; + } +} diff --git a/src/main/java/g0601_0700/s0630_course_schedule_iii/readme.md b/src/main/java/g0601_0700/s0630_course_schedule_iii/readme.md new file mode 100644 index 000000000..29563d6f9 --- /dev/null +++ b/src/main/java/g0601_0700/s0630_course_schedule_iii/readme.md @@ -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 courses[i] = [durationi, lastDayi] indicate that the ith course should be taken **continuously** for durationi days and must be finished before or on lastDayi. + +You will start on the 1st 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 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day. Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day. Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day. The 4th course cannot be taken now, since you will finish it on the 3300th 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:** + +* 1 <= courses.length <= 104 +* 1 <= durationi, lastDayi <= 104 \ No newline at end of file diff --git a/src/main/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/Solution.java b/src/main/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/Solution.java new file mode 100644 index 000000000..2bcc65572 --- /dev/null +++ b/src/main/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/Solution.java @@ -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
{ + 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> nums) { + PriorityQueue
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; + } +} diff --git a/src/main/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/readme.md b/src/main/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/readme.md new file mode 100644 index 000000000..e19796c16 --- /dev/null +++ b/src/main/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/readme.md @@ -0,0 +1,29 @@ +632\. Smallest Range Covering Elements from K Lists + +Hard + +You have `k` lists of sorted integers in **non-decreasing order**. Find the **smallest** range that includes at least one number from each of the `k` lists. + +We define the range `[a, b]` is smaller than range `[c, d]` if `b - a < d - c` **or** `a < c` if `b - a == d - c`. + +**Example 1:** + +**Input:** nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]] + +**Output:** [20,24] + +**Explanation:** List 1: [4, 10, 15, 24,26], 24 is in range [20,24]. List 2: [0, 9, 12, 20], 20 is in range [20,24]. List 3: [5, 18, 22, 30], 22 is in range [20,24]. + +**Example 2:** + +**Input:** nums = [[1,2,3],[1,2,3],[1,2,3]] + +**Output:** [1,1] + +**Constraints:** + +* `nums.length == k` +* `1 <= k <= 3500` +* `1 <= nums[i].length <= 50` +* -105 <= nums[i][j] <= 105 +* `nums[i]` is sorted in **non-decreasing** order. \ No newline at end of file diff --git a/src/test/java/g0601_0700/s0623_add_one_row_to_tree/SolutionTest.java b/src/test/java/g0601_0700/s0623_add_one_row_to_tree/SolutionTest.java new file mode 100644 index 000000000..9baa4f11d --- /dev/null +++ b/src/test/java/g0601_0700/s0623_add_one_row_to_tree/SolutionTest.java @@ -0,0 +1,26 @@ +package g0601_0700.s0623_add_one_row_to_tree; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import com_github_leetcode.TreeNode; +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void addOneRow() { + TreeNode treeNode = TreeNode.create(Arrays.asList(4, 2, 6, 3, 1, 5)); + TreeNode expected = TreeNode.create(Arrays.asList(4, 1, 1, 2, null, null, 6, 3, 1, 5)); + assertThat( + new Solution().addOneRow(treeNode, 1, 2).toString(), equalTo(expected.toString())); + } + + @Test + void addOneRow2() { + TreeNode treeNode = TreeNode.create(Arrays.asList(4, 2, null, 3, 1)); + TreeNode expected = TreeNode.create(Arrays.asList(4, 2, null, 1, 1, 3, null, null, 1)); + assertThat( + new Solution().addOneRow(treeNode, 1, 3).toString(), equalTo(expected.toString())); + } +} diff --git a/src/test/java/g0601_0700/s0628_maximum_product_of_three_numbers/SolutionTest.java b/src/test/java/g0601_0700/s0628_maximum_product_of_three_numbers/SolutionTest.java new file mode 100644 index 000000000..84a34aa91 --- /dev/null +++ b/src/test/java/g0601_0700/s0628_maximum_product_of_three_numbers/SolutionTest.java @@ -0,0 +1,23 @@ +package g0601_0700.s0628_maximum_product_of_three_numbers; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumProduct() { + assertThat(new Solution().maximumProduct(new int[] {1, 2, 3}), equalTo(6)); + } + + @Test + void maximumProduct2() { + assertThat(new Solution().maximumProduct(new int[] {1, 2, 3, 4}), equalTo(24)); + } + + @Test + void maximumProduct3() { + assertThat(new Solution().maximumProduct(new int[] {-1, -2, -3}), equalTo(-6)); + } +} diff --git a/src/test/java/g0601_0700/s0629_k_inverse_pairs_array/SolutionTest.java b/src/test/java/g0601_0700/s0629_k_inverse_pairs_array/SolutionTest.java new file mode 100644 index 000000000..142eda2a2 --- /dev/null +++ b/src/test/java/g0601_0700/s0629_k_inverse_pairs_array/SolutionTest.java @@ -0,0 +1,18 @@ +package g0601_0700.s0629_k_inverse_pairs_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void kInversePairs() { + assertThat(new Solution().kInversePairs(3, 0), equalTo(1)); + } + + @Test + void kInversePairs2() { + assertThat(new Solution().kInversePairs(3, 1), equalTo(2)); + } +} diff --git a/src/test/java/g0601_0700/s0630_course_schedule_iii/SolutionTest.java b/src/test/java/g0601_0700/s0630_course_schedule_iii/SolutionTest.java new file mode 100644 index 000000000..47cb62291 --- /dev/null +++ b/src/test/java/g0601_0700/s0630_course_schedule_iii/SolutionTest.java @@ -0,0 +1,24 @@ +package g0601_0700.s0630_course_schedule_iii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void scheduleCourse() { + int[][] input = new int[][] {{100, 200}, {200, 1300}, {1000, 1250}, {2000, 3200}}; + assertThat(new Solution().scheduleCourse(input), equalTo(3)); + } + + @Test + void scheduleCourse2() { + assertThat(new Solution().scheduleCourse(new int[][] {{1, 2}}), equalTo(1)); + } + + @Test + void scheduleCourse3() { + assertThat(new Solution().scheduleCourse(new int[][] {{3, 2}, {4, 3}}), equalTo(0)); + } +} diff --git a/src/test/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/SolutionTest.java b/src/test/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/SolutionTest.java new file mode 100644 index 000000000..0697c8f09 --- /dev/null +++ b/src/test/java/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/SolutionTest.java @@ -0,0 +1,33 @@ +package g0601_0700.s0632_smallest_range_covering_elements_from_k_lists; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void smallestRange() { + assertThat( + new Solution() + .smallestRange( + Arrays.asList( + Arrays.asList(4, 10, 15, 24, 26), + Arrays.asList(0, 9, 12, 20), + Arrays.asList(5, 18, 22, 30))), + equalTo(new int[] {24, 20})); + } + + @Test + void smallestRange2() { + assertThat( + new Solution() + .smallestRange( + Arrays.asList( + Arrays.asList(1, 2, 3), + Arrays.asList(1, 2, 3), + Arrays.asList(1, 2, 3))), + equalTo(new int[] {1, 1})); + } +} From e29ba36e7b8e57660557d4c05c5a75031ea513c4 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: 2022年1月13日 08:41:21 +0700 Subject: [PATCH 2/3] fix smell --- .../s0629_k_inverse_pairs_array/Solution.java | 8 +- .../s0630_course_schedule_iii/Solution.java | 98 +++++++++---------- 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java b/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java index fa1f8eaac..ccc7501e8 100644 --- a/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java +++ b/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java @@ -12,14 +12,16 @@ public int kInversePairs(int n, int k) { int[] dp1 = new int[k + 1]; dp[0] = 1; dp1[0] = 1; - int MOD = 1_000_000_007; + 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; + dp[j] = (dp1[j] + dp[j - 1] - (j>= i ? dp1[j - i] : 0)) % mod; + if (dp[j] < 0) { + dp[j] += mod; + } } } return dp[k]; diff --git a/src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java b/src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java index d099930f3..517526475 100644 --- a/src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java +++ b/src/main/java/g0601_0700/s0630_course_schedule_iii/Solution.java @@ -23,71 +23,71 @@ public int scheduleCourse(int[][] courses) { } return course; } -} -class MaxHeap { - private final int[] heap; - private int pin; + static class MaxHeap { + private final int[] heap; + private int pin; - public MaxHeap(int mexLen) { - this.heap = new int[mexLen]; - this.pin = 0; - } + public MaxHeap(int mexLen) { + this.heap = new int[mexLen]; + this.pin = 0; + } - public int[] getHeap() { - return heap; - } + public int[] getHeap() { + return heap; + } - public int getPin() { - return pin; - } + public int getPin() { + return pin; + } - public void setPin(int pin) { - this.pin = 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 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]) { + 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 { - heap[temp] = heap[temp * 2 + 2]; - temp = temp * 2 + 2; - heap[temp] = h0; + break; } } else { - break; + 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; } - return res; } } From 23388fdf497e62dc5fb59850c263a80068ef0269 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: 2022年1月13日 13:08:56 +0700 Subject: [PATCH 3/3] fix comments --- .../g0601_0700/s0623_add_one_row_to_tree/Solution.java | 2 ++ .../s0628_maximum_product_of_three_numbers/Solution.java | 2 ++ .../g0601_0700/s0629_k_inverse_pairs_array/Solution.java | 2 +- .../s0629_k_inverse_pairs_array/SolutionTest.java | 8 ++++---- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java b/src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java index 648752f10..1fd9ef059 100644 --- a/src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java +++ b/src/main/java/g0601_0700/s0623_add_one_row_to_tree/Solution.java @@ -1,5 +1,7 @@ package g0601_0700.s0623_add_one_row_to_tree; +// #Medium #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree + import com_github_leetcode.TreeNode; public class Solution { diff --git a/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.java b/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.java index f5dad409c..6bc9fef33 100644 --- a/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.java +++ b/src/main/java/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.java @@ -1,5 +1,7 @@ package g0601_0700.s0628_maximum_product_of_three_numbers; +// #Easy #Array #Math #Sorting + public class Solution { public int maximumProduct(int[] a) { int min1 = Integer.MAX_VALUE; diff --git a/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java b/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java index ccc7501e8..009836723 100644 --- a/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java +++ b/src/main/java/g0601_0700/s0629_k_inverse_pairs_array/Solution.java @@ -3,7 +3,7 @@ // #Hard #Dynamic_Programming public class Solution { - public int kInversePairs(int n, int k) { + public int kinversepairs(int n, int k) { k = Math.min(k, n * (n - 1) / 2 - k); if (k < 0) { return 0; diff --git a/src/test/java/g0601_0700/s0629_k_inverse_pairs_array/SolutionTest.java b/src/test/java/g0601_0700/s0629_k_inverse_pairs_array/SolutionTest.java index 142eda2a2..e61b23733 100644 --- a/src/test/java/g0601_0700/s0629_k_inverse_pairs_array/SolutionTest.java +++ b/src/test/java/g0601_0700/s0629_k_inverse_pairs_array/SolutionTest.java @@ -7,12 +7,12 @@ class SolutionTest { @Test - void kInversePairs() { - assertThat(new Solution().kInversePairs(3, 0), equalTo(1)); + void kinversepairs() { + assertThat(new Solution().kinversepairs(3, 0), equalTo(1)); } @Test - void kInversePairs2() { - assertThat(new Solution().kInversePairs(3, 1), equalTo(2)); + void kinversepairs2() { + assertThat(new Solution().kinversepairs(3, 1), equalTo(2)); } }

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