diff --git a/README.md b/README.md index ccbc2ec36..c2fb3cf65 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,15 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1467 | [Probability of a Two Boxes Having The Same Number of Distinct Balls](https://leetcode.com/problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls "两个盒子中不同颜色的球数量相同的概率") | [Go](problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls) | Hard | +| 1466 | [Reorder Routes to Make All Paths Lead to the City Zero](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero "重新规划路线") | [Go](problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero) | Medium | +| 1465 | [Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts "切割后面积最大的蛋糕") | [Go](problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts) | Medium | +| 1464 | [Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array "数组中两元素的最大乘积") | [Go](problems/maximum-product-of-two-elements-in-an-array) | Easy | +| 1463 | [Cherry Pickup II](https://leetcode.com/problems/cherry-pickup-ii "摘樱桃 II") | [Go](problems/cherry-pickup-ii) | Hard | +| 1462 | [Course Schedule IV](https://leetcode.com/problems/course-schedule-iv "课程安排 IV") | [Go](problems/course-schedule-iv) | Medium | +| 1461 | [Check If a String Contains All Binary Codes of Size K](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k "检查一个字符串是否包含所有大小为 K 的二进制子串") | [Go](problems/check-if-a-string-contains-all-binary-codes-of-size-k) | Medium | +| 1460 | [Make Two Arrays Equal by Reversing Sub-arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays "通过翻转子数组使两个数组相等") | [Go](problems/make-two-arrays-equal-by-reversing-sub-arrays) | Easy | +| 1459 | [Rectangles Area](https://leetcode.com/problems/rectangles-area) 🔒 | [MySQL](problems/rectangles-area) | Medium | | 1458 | [Max Dot Product of Two Subsequences](https://leetcode.com/problems/max-dot-product-of-two-subsequences "两个子序列的最大点积") | [Go](problems/max-dot-product-of-two-subsequences) | Hard | | 1457 | [Pseudo-Palindromic Paths in a Binary Tree](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree "二叉树中的伪回文路径") | [Go](problems/pseudo-palindromic-paths-in-a-binary-tree) | Medium | | 1456 | [Maximum Number of Vowels in a Substring of Given Length](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length "定长子串中元音的最大数目") | [Go](problems/maximum-number-of-vowels-in-a-substring-of-given-length) | Medium | @@ -80,7 +89,7 @@ LeetCode Problems' Solutions | 1443 | [Minimum Time to Collect All Apples in a Tree](https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree "收集树上所有苹果的最少时间") | [Go](problems/minimum-time-to-collect-all-apples-in-a-tree) | Medium | | 1442 | [Count Triplets That Can Form Two Arrays of Equal XOR](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor "形成两个异或相等数组的三元组数目") | [Go](problems/count-triplets-that-can-form-two-arrays-of-equal-xor) | Medium | | 1441 | [Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations "用栈操作构建数组") | [Go](problems/build-an-array-with-stack-operations) | Easy | -| 1440 | [Evaluate Boolean Expression](https://leetcode.com/problems/evaluate-boolean-expression) 🔒 | [MySQL](problems/evaluate-boolean-expression) | Medium | +| 1440 | [Evaluate Boolean Expression](https://leetcode.com/problems/evaluate-boolean-expression "计算布尔表达式的值") 🔒 | [MySQL](problems/evaluate-boolean-expression) | Medium | | 1439 | [Find the Kth Smallest Sum of a Matrix With Sorted Rows](https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows "有序矩阵中的第 k 个最小数组和") | [Go](problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows) | Hard | | 1438 | [Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit "绝对差不超过限制的最长连续子数组") | [Go](problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) | Medium | | 1437 | [Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away "是否所有 1 都至少相隔 k 个元素") | [Go](problems/check-if-all-1s-are-at-least-length-k-places-away) | Medium | diff --git a/problems/3sum-closest/README.md b/problems/3sum-closest/README.md index e5a964119..d73441b68 100644 --- a/problems/3sum-closest/README.md +++ b/problems/3sum-closest/README.md @@ -13,14 +13,24 @@
Given an array nums
of n integers and an integer target
, find three integers in nums
such that the sum is closest to target
. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
+ +Example 1:
-Given array nums = [-1, 2, 1, -4], and target = 1. - -The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). +Input: nums = [-1,2,1,-4], target = 1 +Output: 2 +Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).+ +
Constraints:
+ +3 <= nums.length <= 10^3
-10^3 <= nums[i] <= 10^3
-10^4 <= target <= 10^4
Given a binary string s
and an integer k
.
Return True if all binary codes of length k
is a substring of s
. Otherwise, return False.
Example 1:
+ ++Input: s = "00110110", k = 2 +Output: true +Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. ++ +
Example 2:
+ ++Input: s = "00110", k = 2 +Output: true ++ +
Example 3:
+ ++Input: s = "0110", k = 1 +Output: true +Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. ++ +
Example 4:
+ ++Input: s = "0110", k = 2 +Output: false +Explanation: The binary code "00" is of length 2 and doesn't exist in the array. ++ +
Example 5:
+ ++Input: s = "0000000001011100", k = 4 +Output: false ++ + +
Constraints:
+ +1 <= s.length <= 5 * 10^5
s
consists of 0's and 1's only.1 <= k <= 20
Given a rows x cols
matrix grid
representing a field of cherries. Each cell in grid
represents the number of cherries that you can collect.
You have two robots that can collect cherries for you, Robot #1 is located at the top-left corner (0,0) , and Robot #2 is located at the top-right corner (0, cols-1) of the grid.
+ +Return the maximum number of cherries collection using both robots by following the rules below:
+ +grid
.Example 1:
+ + + ++Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] +Output: 24 +Explanation: Path of robot #1 and #2 are described in color green and blue respectively. +Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. +Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. +Total of cherries: 12 + 12 = 24. ++ +
Example 2:
+ + + ++Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]] +Output: 28 +Explanation: Path of robot #1 and #2 are described in color green and blue respectively. +Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17. +Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11. +Total of cherries: 17 + 11 = 28. ++ +
Example 3:
+ ++Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]] +Output: 22 ++ +
Example 4:
+ ++Input: grid = [[1,1],[1,1]] +Output: 4 ++ + +
Constraints:
+ +rows == grid.length
cols == grid[i].length
2 <= rows, cols <= 70
0 <= grid[i][j] <= 100
A cinema has n
rows of seats, numbered from 1 to n
and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above.
Given the array reservedSeats
containing the numbers of seats already reserved, for example, reservedSeats[i]=[3,8]
means the seat located in row 3 and labelled with 8 is already reserved.
Given the array reservedSeats
containing the numbers of seats already reserved, for example, reservedSeats[i] = [3,8]
means the seat located in row 3 and labelled with 8 is already reserved.
Return the maximum number of four-person families you can allocate on the cinema seats. A four-person family occupies fours seats in one row, that are next to each other. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be next to each other, however, It is permissible for the four-person family to be separated by an aisle, but in that case, exactly two people have to sit on each side of the aisle.
+Return the maximum number of four-person groups you can assign on the cinema seats. A four-person group occupies four adjacent seats in one single row. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be adjacent, but there is an exceptional case on which an aisle split a four-person group, in that case, the aisle split a four-person group in the middle, which means to have two people on each side.
Example 1:
@@ -27,7 +27,7 @@Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]] Output: 4 -Explanation: The figure above shows the optimal allocation for four families, where seats mark with blue are already reserved and contiguous seats mark with orange are for one family. +Explanation: The figure above shows the optimal allocation for four groups, where seats mark with blue are already reserved and contiguous seats mark with orange are for one group.
Example 2:
diff --git a/problems/course-schedule-iv/README.md b/problems/course-schedule-iv/README.md new file mode 100644 index 000000000..75eee50ff --- /dev/null +++ b/problems/course-schedule-iv/README.md @@ -0,0 +1,95 @@ + + + + + + + +[< Previous](../check-if-a-string-contains-all-binary-codes-of-size-k "Check If a String Contains All Binary Codes of Size K") + +[Next>](../cherry-pickup-ii "Cherry Pickup II") + +## [1462. Course Schedule IV (Medium)](https://leetcode.com/problems/course-schedule-iv "课程安排 IV") + +There are a total of n
courses you have to take, labeled from 0
to n-1
.
Some courses may have direct prerequisites, for example, to take course 0 you have first to take course 1, which is expressed as a pair: [1,0]
Given the total number of courses n
, a list of direct prerequisite
pairs and a list of queries
pairs.
You should answer for each queries[i]
whether the course queries[i][0]
is a prerequisite of the course queries[i][1]
or not.
Return a list of boolean, the answers to the given queries
.
Please note that if course a is a prerequisite of course b and course b is a prerequisite of course c, then, course a is a prerequisite of course c.
+ + +Example 1:
+ ++Input: n = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]] +Output: [false,true] +Explanation: course 0 is not a prerequisite of course 1 but the opposite is true. ++ +
Example 2:
+ ++Input: n = 2, prerequisites = [], queries = [[1,0],[0,1]] +Output: [false,false] +Explanation: There are no prerequisites and each course is independent. ++ +
Example 3:
+ ++Input: n = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]] +Output: [true,true] ++ +
Example 4:
+ ++Input: n = 3, prerequisites = [[1,0],[2,0]], queries = [[0,1],[2,0]] +Output: [false,true] ++ +
Example 5:
+ ++Input: n = 5, prerequisites = [[0,1],[1,2],[2,3],[3,4]], queries = [[0,4],[4,0],[1,3],[3,0]] +Output: [true,false,true,false] ++ + +
Constraints:
+ +2 <= n <= 100
0 <= prerequisite.length <= (n * (n - 1) / 2)
0 <= prerequisite[i][0], prerequisite[i][1] < n
prerequisite[i][0] != prerequisite[i][1]
1 <= queries.length <= 10^4
queries[i][0] != queries[i][1]
True
if its possibleotherwi
1 <= nums[i] <= 10^9
1 <= k <= nums.length
Input: hand = [1,2,3,4,5], W = 4 Output: false -Explanation: Alice's+Explanation: Alice'shand
can't be rearranged into groups of4
.
hand
can't be rearranged into groups of 4
.
+
+Constraints:
-Note:
- -1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]] Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]] -Reminder: The inputs and the desired output are lists of Interval objects, and not arrays or lists.@@ -39,8 +38,6 @@
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
### Related Topics diff --git a/problems/make-two-arrays-equal-by-reversing-sub-arrays/README.md b/problems/make-two-arrays-equal-by-reversing-sub-arrays/README.md new file mode 100644 index 000000000..2ec259377 --- /dev/null +++ b/problems/make-two-arrays-equal-by-reversing-sub-arrays/README.md @@ -0,0 +1,85 @@ + + + + + + + +[< Previous](../rectangles-area "Rectangles Area") + +[Next>](../check-if-a-string-contains-all-binary-codes-of-size-k "Check If a String Contains All Binary Codes of Size K") + +## [1460. Make Two Arrays Equal by Reversing Sub-arrays (Easy)](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays "通过翻转子数组使两个数组相等") + +Given two integer arrays of equal length target
and arr
.
In one step, you can select any non-empty sub-array of arr
and reverse it. You are allowed to make any number of steps.
Return True if you can make arr
equal to target
, or False otherwise.
Example 1:
+ ++Input: target = [1,2,3,4], arr = [2,4,1,3] +Output: true +Explanation: You can follow the next steps to convert arr to target: +1- Reverse sub-array [2,4,1], arr becomes [1,4,2,3] +2- Reverse sub-array [4,2], arr becomes [1,2,4,3] +3- Reverse sub-array [4,3], arr becomes [1,2,3,4] +There are multiple ways to convert arr to target, this is not the only way to do so. ++ +
Example 2:
+ ++Input: target = [7], arr = [7] +Output: true +Explanation: arr is equal to target without any reverses. ++ +
Example 3:
+ ++Input: target = [1,12], arr = [12,1] +Output: true ++ +
Example 4:
+ ++Input: target = [3,7,9], arr = [3,7,11] +Output: false +Explanation: arr doesn't have value 9 and it can never be converted to target. ++ +
Example 5:
+ ++Input: target = [1,1,1,1,1], arr = [1,1,1,1,1] +Output: true ++ + +
Constraints:
+ +target.length == arr.length
1 <= target.length <= 1000
1 <= target[i] <= 1000
1 <= arr[i] <= 1000
Given a rectangular cake with height h
and width w
, and two arrays of integers horizontalCuts
and verticalCuts
where horizontalCuts[i]
is the distance from the top of the rectangular cake to the ith
horizontal cut and similarly, verticalCuts[j]
is the distance from the left of the rectangular cake to the jth
vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts
and verticalCuts
. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
+ + + ++Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] +Output: 4 +Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. ++ +
Example 2:
+ + + ++Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] +Output: 6 +Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. ++ +
Example 3:
+ ++Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] +Output: 9 ++ + +
Constraints:
+ +2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
horizontalCuts
are distinct.verticalCuts
are distinct.nums
, you will choose two different indices i
and j
of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1)
.
+
+Example 1:
+ ++Input: nums = [3,4,5,2] +Output: 12 +Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. ++ +
Example 2:
+ ++Input: nums = [1,5,4,5] +Output: 16 +Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16. ++ +
Example 3:
+ ++Input: nums = [3,7] +Output: 12 ++ + +
Constraints:
+ +2 <= nums.length <= 500
1 <= nums[i] <= 10^3
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
+Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.
+ +Example:
-Example:
-Input: 1->2->4, 1->3->4 -Output: 1->1->2->3->4->4 +Input: 1->2->4, 1->3->4 +Output: 1->1->2->3->4->4- ### Related Topics [[Linked List](../../tag/linked-list/README.md)] diff --git a/problems/possible-bipartition/README.md b/problems/possible-bipartition/README.md index 55fbc1546..251cd5219 100644 --- a/problems/possible-bipartition/README.md +++ b/problems/possible-bipartition/README.md @@ -52,21 +52,21 @@ Input: N = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]] Output: false + + + +
Constraints:
-Note:
- -1 <= N <= 2000
0 <= dislikes.length <= 10000
dislikes[i].length == 2
1 <= dislikes[i][j] <= N
dislikes[i][0] < dislikes[i][1]
i != j
for which dislikes[i] == dislikes[j]
.Given 2n
balls of k
distinct colors. You will be given an integer array balls
of size k
where balls[i]
is the number of balls of color i
.
All the balls will be shuffled uniformly at random, then we will distribute the first n
balls to the first box and the remaining n
balls to the other box (Please read the explanation of the second example carefully).
Please note that the two boxes are considered different. For example, if we have two balls of colors a
and b
, and two boxes []
and ()
, then the distribution [a] (b)
is considered different than the distribution [b] (a)
(Please read the explanation of the first example carefully).
We want to calculate the probability that the two boxes have the same number of distinct balls.
+ + +Example 1:
+ ++Input: balls = [1,1] +Output: 1.00000 +Explanation: Only 2 ways to divide the balls equally: +- A ball of color 1 to box 1 and a ball of color 2 to box 2 +- A ball of color 2 to box 1 and a ball of color 1 to box 2 +In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 ++ +
Example 2:
+ ++Input: balls = [2,1,1] +Output: 0.66667 +Explanation: We have the set of balls [1, 1, 2, 3] +This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): +[1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] +After that we add the first two balls to the first box and the second two balls to the second box. +We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. +Probability is 8/12 = 0.66667 ++ +
Example 3:
+ ++Input: balls = [1,2,1,2] +Output: 0.60000 +Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. +Probability = 108 / 180 = 0.6 ++ +
Example 4:
+ ++Input: balls = [3,2,1] +Output: 0.30000 +Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. +Probability = 18 / 60 = 0.3 ++ +
Example 5:
+ ++Input: balls = [6,6,6,6,6,6] +Output: 0.90327 ++ + +
Constraints:
+ +1 <= balls.length <= 8
1 <= balls[i] <= 6
sum(balls)
is even.10^-5
of the actual value will be accepted as correct.Constraints:
-Note:
- -bottom
will be a string with length in range [2, 8]
.allowed
will have length in range [0, 200]
.{'A', 'B', 'C', 'D', 'E', 'F', 'G'}
.There are n
cities numbered from 0
to n-1
and n-1
roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.
Roads are represented by connections
where connections[i] = [a, b]
represents a road from city a
to b
.
This year, there will be a big event in the capital (city 0), and many people want to travel to this city.
+ +Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed.
+ +It's guaranteed that each city can reach the city 0 after reorder.
+ + +Example 1:
+ + + ++Input: n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]] +Output: 3 +Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).+ +
Example 2:
+ + + ++Input: n = 5, connections = [[1,0],[1,2],[3,2],[3,4]] +Output: 2 +Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).+ +
Example 3:
+ ++Input: n = 3, connections = [[1,0],[2,0]] +Output: 0 ++ + +
Constraints:
+ +2 <= n <= 5 * 10^4
connections.length == n-1
connections[i].length == 2
0 <= connections[i][0], connections[i][1] <= n-1
connections[i][0] != connections[i][1]
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
+Given an integer n
, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
Example:
@@ -35,6 +35,13 @@ The above output corresponds to the 5 unique BST's shown below: 2 1 2 3 + +Constraints:
+ +0 <= n <= 8
Constraints:
-Note:
- -1 <= clips.length <= 100
0 <= clips[i][0], clips[i][1] <= 100
0 <= clips[i][0] <= clips[i][1] <= 100
0 <= T <= 100