From aad78465f3c789e998f3afe1a065f57708d56aff Mon Sep 17 00:00:00 2001 From: Shuo Date: 2020年5月31日 18:45:38 +0800 Subject: [PATCH] A: new --- README.md | 11 +- problems/3sum-closest/README.md | 18 ++- .../README.md | 79 +++++++++++++ problems/cherry-pickup-ii/README.md | 86 ++++++++++++++ problems/cinema-seat-allocation/README.md | 6 +- problems/course-schedule-iv/README.md | 95 ++++++++++++++++ .../README.md | 1 + .../evaluate-boolean-expression/README.md | 2 +- problems/hand-of-straights/README.md | 11 +- .../interval-list-intersections/README.md | 3 - .../README.md | 85 ++++++++++++++ .../README.md | 2 +- .../README.md | 71 ++++++++++++ .../README.md | 54 +++++++++ problems/merge-two-sorted-lists/README.md | 10 +- problems/possible-bipartition/README.md | 14 +-- .../README.md | 105 ++++++++++++++++++ problems/pyramid-transition-matrix/README.md | 9 +- problems/rectangles-area/README.md | 14 +++ problems/rectangles-area/mysql_schemas.sql | 5 + .../README.md | 69 ++++++++++++ .../unique-binary-search-trees-ii/README.md | 9 +- problems/video-stitching/README.md | 9 +- 23 files changed, 726 insertions(+), 42 deletions(-) create mode 100644 problems/check-if-a-string-contains-all-binary-codes-of-size-k/README.md create mode 100644 problems/cherry-pickup-ii/README.md create mode 100644 problems/course-schedule-iv/README.md create mode 100644 problems/make-two-arrays-equal-by-reversing-sub-arrays/README.md create mode 100644 problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/README.md create mode 100644 problems/maximum-product-of-two-elements-in-an-array/README.md create mode 100644 problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls/README.md create mode 100644 problems/rectangles-area/README.md create mode 100644 problems/rectangles-area/mysql_schemas.sql create mode 100644 problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md 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:

+ + + ### Related Topics [[Array](../../tag/array/README.md)] [[Two Pointers](../../tag/two-pointers/README.md)] diff --git a/problems/check-if-a-string-contains-all-binary-codes-of-size-k/README.md b/problems/check-if-a-string-contains-all-binary-codes-of-size-k/README.md new file mode 100644 index 000000000..d50ff2544 --- /dev/null +++ b/problems/check-if-a-string-contains-all-binary-codes-of-size-k/README.md @@ -0,0 +1,79 @@ + + + + + + + +[< Previous](../make-two-arrays-equal-by-reversing-sub-arrays "Make Two Arrays Equal by Reversing Sub-arrays") + +[Next>](../course-schedule-iv "Course Schedule IV") + +## [1461. Check If a String Contains All Binary Codes of Size K (Medium)](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k "检查一个字符串是否包含所有长度为 K 的二进制子串") + +

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:

+ + + +### Related Topics + [[Bit Manipulation](../../tag/bit-manipulation/README.md)] + [[String](../../tag/string/README.md)] + +### Hints +
+Hint 1 +We need only to check all sub-strings of length k. +
+ +
+Hint 2 +The number of distinct sub-strings should be exactly 2^k. +
diff --git a/problems/cherry-pickup-ii/README.md b/problems/cherry-pickup-ii/README.md new file mode 100644 index 000000000..d3eb098d8 --- /dev/null +++ b/problems/cherry-pickup-ii/README.md @@ -0,0 +1,86 @@ + + + + + + + +[< Previous](../course-schedule-iv "Course Schedule IV") + +[Next>](../maximum-product-of-two-elements-in-an-array "Maximum Product of Two Elements in an Array") + +## [1463. Cherry Pickup II (Hard)](https://leetcode.com/problems/cherry-pickup-ii "摘樱桃 II") + +

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:

+ + + + +

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:

+ + + +### Related Topics + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Use dynammic programming, define DP[i][j][k]: The maximum cherries that both robots can take starting on the ith row, and column j and k of Robot 1 and 2 respectively. +
diff --git a/problems/cinema-seat-allocation/README.md b/problems/cinema-seat-allocation/README.md index 1825bddea..b3cfc5d7a 100644 --- a/problems/cinema-seat-allocation/README.md +++ b/problems/cinema-seat-allocation/README.md @@ -15,9 +15,9 @@

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:

+ + + +### Related Topics + [[Graph](../../tag/graph/README.md)] + +### Hints +
+Hint 1 +Imagine if the courses are nodes of a graph. We need to build an array isReachable[i][j]. +
+ +
+Hint 2 +Start a bfs from each course i and assign for each course j you visit isReachable[i][j] = True. +
+ +
+Hint 3 +Answer the queries from the isReachable array. +
diff --git a/problems/divide-array-in-sets-of-k-consecutive-numbers/README.md b/problems/divide-array-in-sets-of-k-consecutive-numbers/README.md index a0d5c312b..b5867e953 100644 --- a/problems/divide-array-in-sets-of-k-consecutive-numbers/README.md +++ b/problems/divide-array-in-sets-of-k-consecutive-numbers/README.md @@ -54,6 +54,7 @@ Return True if its possibleotherwi
  • 1 <= nums[i] <= 10^9
  • 1 <= k <= nums.length
  • +Note: This question is the same as 846: https://leetcode.com/problems/hand-of-straights/ ### Related Topics [[Greedy](../../tag/greedy/README.md)] diff --git a/problems/evaluate-boolean-expression/README.md b/problems/evaluate-boolean-expression/README.md index b3ccf69e9..c2f7c6614 100644 --- a/problems/evaluate-boolean-expression/README.md +++ b/problems/evaluate-boolean-expression/README.md @@ -9,6 +9,6 @@ [Next>](../build-an-array-with-stack-operations "Build an Array With Stack Operations") -## [1440. Evaluate Boolean Expression (Medium)](https://leetcode.com/problems/evaluate-boolean-expression "") +## [1440. Evaluate Boolean Expression (Medium)](https://leetcode.com/problems/evaluate-boolean-expression "计算布尔表达式的值") diff --git a/problems/hand-of-straights/README.md b/problems/hand-of-straights/README.md index fe56a4df6..f3999aa27 100644 --- a/problems/hand-of-straights/README.md +++ b/problems/hand-of-straights/README.md @@ -34,17 +34,18 @@
     Input: hand = [1,2,3,4,5], W = 4
     Output: false
    -Explanation: Alice's hand can't be rearranged into groups of 4.
    +Explanation: Alice's hand can't be rearranged into groups of 4. + +

    Constraints:

    -

    Note:

    - -
      +
    + +Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ ### Related Topics [[Ordered Map](../../tag/ordered-map/README.md)] diff --git a/problems/interval-list-intersections/README.md b/problems/interval-list-intersections/README.md index 5b31fbeca..b394cb21e 100644 --- a/problems/interval-list-intersections/README.md +++ b/problems/interval-list-intersections/README.md @@ -27,7 +27,6 @@
     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:

    + + + +### Related Topics + [[Array](../../tag/array/README.md)] + +### Hints +
    +Hint 1 +Each element of target should have a corresponding element in arr, and if it doesn't have a corresponding element, return false. +
    + +
    +Hint 2 +To solve it easiely you can sort the two arrays and check if they are equal. +
    diff --git a/problems/max-dot-product-of-two-subsequences/README.md b/problems/max-dot-product-of-two-subsequences/README.md index ee6d8c975..a756469e9 100644 --- a/problems/max-dot-product-of-two-subsequences/README.md +++ b/problems/max-dot-product-of-two-subsequences/README.md @@ -7,7 +7,7 @@ [< Previous](../pseudo-palindromic-paths-in-a-binary-tree "Pseudo-Palindromic Paths in a Binary Tree") -Next> +[Next>](../rectangles-area "Rectangles Area") ## [1458. Max Dot Product of Two Subsequences (Hard)](https://leetcode.com/problems/max-dot-product-of-two-subsequences "两个子序列的最大点积") diff --git a/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/README.md b/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/README.md new file mode 100644 index 000000000..188966c7f --- /dev/null +++ b/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/README.md @@ -0,0 +1,71 @@ + + + + + + + +[< Previous](../maximum-product-of-two-elements-in-an-array "Maximum Product of Two Elements in an Array") + +[Next>](../reorder-routes-to-make-all-paths-lead-to-the-city-zero "Reorder Routes to Make All Paths Lead to the City Zero") + +## [1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (Medium)](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts "切割后面积最大的蛋糕") + +

    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:

    + + + +### Related Topics + [[Array](../../tag/array/README.md)] + +### Hints +
    +Hint 1 +Sort the arrays, then compute the maximum difference between two consecutive elements for horizontal cuts and vertical cuts. +
    + +
    +Hint 2 +The answer is the product of these maximum values in horizontal cuts and vertical cuts. +
    diff --git a/problems/maximum-product-of-two-elements-in-an-array/README.md b/problems/maximum-product-of-two-elements-in-an-array/README.md new file mode 100644 index 000000000..f343dba97 --- /dev/null +++ b/problems/maximum-product-of-two-elements-in-an-array/README.md @@ -0,0 +1,54 @@ + + + + + + + +[< Previous](../cherry-pickup-ii "Cherry Pickup II") + +[Next>](../maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts "Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts") + +## [1464. Maximum Product of Two Elements in an Array (Easy)](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array "数组中两元素的最大乘积") + +Given the array of integers 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:

    + + + +### Related Topics + [[Array](../../tag/array/README.md)] + +### Hints +
    +Hint 1 +Use brute force: two loops to select i and j, then select the maximum value of (nums[i]-1)*(nums[j]-1). +
    diff --git a/problems/merge-two-sorted-lists/README.md b/problems/merge-two-sorted-lists/README.md index 565a0b7e3..c60d75e2a 100644 --- a/problems/merge-two-sorted-lists/README.md +++ b/problems/merge-two-sorted-lists/README.md @@ -11,14 +11,14 @@ ## [21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists "合并两个有序链表") -

    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:

    - -
      +
    - - - + ### Related Topics [[Depth-first Search](../../tag/depth-first-search/README.md)] diff --git a/problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls/README.md b/problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls/README.md new file mode 100644 index 000000000..ec4a207df --- /dev/null +++ b/problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls/README.md @@ -0,0 +1,105 @@ + + + + + + + +[< Previous](../reorder-routes-to-make-all-paths-lead-to-the-city-zero "Reorder Routes to Make All Paths Lead to the City Zero") + +Next> + +## [1467. Probability of a Two Boxes Having The Same Number of Distinct Balls (Hard)](https://leetcode.com/problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls "两个盒子中球的颜色数相同的概率") + +

    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:

    + + + +### Related Topics + [[Math](../../tag/math/README.md)] + [[Backtracking](../../tag/backtracking/README.md)] + +### Hints +
    +Hint 1 +Check how many ways you can distribute the balls between the boxes. +
    + +
    +Hint 2 +Consider that one way you will use (x1, x2, x3, ..., xk) where xi is the number of balls from colour i. The probability of achieving this way randomly is ( (ball1 C x1) * (ball2 C x2) * (ball3 C x3) * ... * (ballk C xk)) / (2n C n). +
    + +
    +Hint 3 +The probability of a draw is the sigma of probabilities of different ways to achieve draw. +
    + +
    +Hint 4 +Can you use Dynamic programming to solve this problem in a better complexity ? +
    diff --git a/problems/pyramid-transition-matrix/README.md b/problems/pyramid-transition-matrix/README.md index 4cdb08326..d09d7af8c 100644 --- a/problems/pyramid-transition-matrix/README.md +++ b/problems/pyramid-transition-matrix/README.md @@ -47,16 +47,13 @@ Note that there could be allowed triples (A, B, C) and (A, B, D) with C != D. +

    Constraints:

    -

    Note:

    - -
      +
    - - + ### Related Topics [[Bit Manipulation](../../tag/bit-manipulation/README.md)] diff --git a/problems/rectangles-area/README.md b/problems/rectangles-area/README.md new file mode 100644 index 000000000..cd9a93e2e --- /dev/null +++ b/problems/rectangles-area/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../max-dot-product-of-two-subsequences "Max Dot Product of Two Subsequences") + +[Next>](../make-two-arrays-equal-by-reversing-sub-arrays "Make Two Arrays Equal by Reversing Sub-arrays") + +## [1459. Rectangles Area (Medium)](https://leetcode.com/problems/rectangles-area "") + + diff --git a/problems/rectangles-area/mysql_schemas.sql b/problems/rectangles-area/mysql_schemas.sql new file mode 100644 index 000000000..75f4fb6f3 --- /dev/null +++ b/problems/rectangles-area/mysql_schemas.sql @@ -0,0 +1,5 @@ +Create table If Not Exists Points (id int, x_value int, y_value int); +Truncate table Points; +insert into Points (id, x_value, y_value) values ('1', '2', '8'); +insert into Points (id, x_value, y_value) values ('2', '4', '7'); +insert into Points (id, x_value, y_value) values ('3', '2', '10'); diff --git a/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md b/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md new file mode 100644 index 000000000..8740e846e --- /dev/null +++ b/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md @@ -0,0 +1,69 @@ + + + + + + + +[< Previous](../maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts "Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts") + +[Next>](../probability-of-a-two-boxes-having-the-same-number-of-distinct-balls "Probability of a Two Boxes Having The Same Number of Distinct Balls") + +## [1466. Reorder Routes to Make All Paths Lead to the City Zero (Medium)](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero "重新规划路线") + +

    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:

    + + + +### Related Topics + [[Tree](../../tag/tree/README.md)] + [[Depth-first Search](../../tag/depth-first-search/README.md)] + +### Hints +
    +Hint 1 +Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. +
    diff --git a/problems/unique-binary-search-trees-ii/README.md b/problems/unique-binary-search-trees-ii/README.md index 9fc43863d..482638ce5 100644 --- a/problems/unique-binary-search-trees-ii/README.md +++ b/problems/unique-binary-search-trees-ii/README.md @@ -11,7 +11,7 @@ ## [95. Unique Binary Search Trees II (Medium)](https://leetcode.com/problems/unique-binary-search-trees-ii "不同的二叉搜索树 II") -

    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:

    + + + ### Related Topics [[Tree](../../tag/tree/README.md)] [[Dynamic Programming](../../tag/dynamic-programming/README.md)] diff --git a/problems/video-stitching/README.md b/problems/video-stitching/README.md index e50de6a2e..18472b3d0 100644 --- a/problems/video-stitching/README.md +++ b/problems/video-stitching/README.md @@ -59,14 +59,13 @@ Notice you can have extra video after the event ends. +

    Constraints:

    -

    Note:

    - -
      +
    + ### Related Topics [[Dynamic Programming](../../tag/dynamic-programming/README.md)]

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