From aad78465f3c789e998f3afe1a065f57708d56aff Mon Sep 17 00:00:00 2001
From: Shuo Given an array Example: Example 1: Constraints: Given a binary string Return True if all binary codes of length Example 1: Example 2: Example 3: Example 4: Example 5: Constraints: Given a 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: Example 2: Example 3: Example 4: Constraints: A cinema has Given the array Given the array 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: Example 2: There are a total of 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: Given the total number of courses You should answer for each Return a list of boolean, the answers to the given 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: Example 2: Example 3: Example 4: Example 5: Constraints: Constraints: Note: NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature. Given two integer arrays of equal length In one step, you can select any non-empty sub-array of Return True if you can make Example 1: Example 2: Example 3: Example 4: Example 5: Constraints: Given a rectangular cake with height Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays Example 1: Example 2: Example 3: Constraints: Example 1: Example 2: Example 3: Constraints: 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:
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.
-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).
+
+
+
+
### 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 的二进制子串")
+
+3 <= nums.length <= 10^3
-10^3 <= nums[i] <= 10^3
-10^4 <= target <= 10^4
s
and an integer k
.k
is a substring of s
. Otherwise, return False.
+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.
+
+
+
+Input: s = "00110", k = 2
+Output: true
+
+
+
+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.
+
+
+
+Input: s = "0110", k = 2
+Output: false
+Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
+
+
+
+Input: s = "0000000001011100", k = 4
+Output: false
+
+
+
+
+
+
+### Related Topics
+ [[Bit Manipulation](../../tag/bit-manipulation/README.md)]
+ [[String](../../tag/string/README.md)]
+
+### Hints
+1 <= s.length <= 5 * 10^5
s
consists of 0's and 1's only.1 <= k <= 20
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.
+rows x cols
matrix grid
representing a field of cherries. Each cell in grid
represents the number of cherries that you can collect.
+
+
+
+grid
.
+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.
+
+
+
+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.
+
+
+
+Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]]
+Output: 22
+
+
+
+Input: grid = [[1,1],[1,1]]
+Output: 4
+
+
+
+
+
+
+### Related Topics
+ [[Dynamic Programming](../../tag/dynamic-programming/README.md)]
+
+### Hints
+rows == grid.length
cols == grid[i].length
2 <= rows, cols <= 70
0 <= grid[i][j] <= 100
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.
+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.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. 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.
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.
n
courses you have to take, labeled from 0
to n-1
.[1,0]
n
, a list of direct prerequisite
pairs and a list of queries
pairs.queries[i]
whether the course queries[i][0]
is a prerequisite of the course queries[i][1]
or not.queries
.
+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.
+
+
+
+Input: n = 2, prerequisites = [], queries = [[1,0],[0,1]]
+Output: [false,false]
+Explanation: There are no prerequisites and each course is independent.
+
+
+
+Input: n = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]]
+Output: [true,true]
+
+
+
+Input: n = 3, prerequisites = [[1,0],[2,0]], queries = [[0,1],[2,0]]
+Output: [false,true]
+
+
+
+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]
+
+
+
+
+
+
+### Related Topics
+ [[Graph](../../tag/graph/README.md)]
+
+### Hints
+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]
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.
+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's hand
can't be rearranged into groups of 4
.hand
can't be rearranged into groups of 4
.
+
+
+
+
+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 @@
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
target
and arr
.arr
and reverse it. You are allowed to make any number of steps.arr
equal to target
, or False otherwise.
+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.
+
+
+
+Input: target = [7], arr = [7]
+Output: true
+Explanation: arr is equal to target without any reverses.
+
+
+
+Input: target = [1,12], arr = [12,1]
+Output: true
+
+
+
+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.
+
+
+
+Input: target = [1,1,1,1,1], arr = [1,1,1,1,1]
+Output: true
+
+
+
+
+
+
+### Related Topics
+ [[Array](../../tag/array/README.md)]
+
+### Hints
+target.length == arr.length
1 <= target.length <= 1000
1 <= target[i] <= 1000
1 <= arr[i] <= 1000
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.
+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.horizontalCuts
and verticalCuts
. Since the answer can be a huge number, return this modulo 10^9 + 7.
+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.
+
+
+
+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.
+
+
+
+Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
+Output: 9
+
+
+
+
+
+
+### Related Topics
+ [[Array](../../tag/array/README.md)]
+
+### Hints
+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.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.
+nums
, you will choose two different indices i
and j
of that array. Return the maximum value of (nums[i]-1)*(nums[j]-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.
+
+
+
+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.
+
+
+
+Input: nums = [3,7]
+Output: 12
+
+
+
+
+
+
+### Related Topics
+ [[Array](../../tag/array/README.md)]
+
+### Hints
+2 <= nums.length <= 500
1 <= nums[i] <= 10^3
Hint 1
+Use brute force: two loops to select i and j, then select the maximum value of (nums[i]-1)*(nums[j]-1).
+
-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
-
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