-
Notifications
You must be signed in to change notification settings - Fork 130
A: new #793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
A: new #793
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
problems/check-if-a-string-contains-all-binary-codes-of-size-k/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
<!--|@author openset <openset.wang@gmail.com> |--> | ||
<!--|@link https://github.com/openset |--> | ||
<!--|@home https://github.com/openset/leetcode |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
[< 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 的二进制子串") | ||
|
||
<p>Given a binary string <code>s</code> and an integer <code>k</code>.</p> | ||
|
||
<p>Return <em>True</em> if all binary codes of length <code>k</code> is a substring of <code>s</code>. Otherwise, return <em>False</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "00110110", k = 2 | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> 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. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "00110", k = 2 | ||
<strong>Output:</strong> true | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "0110", k = 1 | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring. | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "0110", k = 2 | ||
<strong>Output:</strong> false | ||
<strong>Explanation:</strong> The binary code "00" is of length 2 and doesn't exist in the array. | ||
</pre> | ||
|
||
<p><strong>Example 5:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "0000000001011100", k = 4 | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length <= 5 * 10^5</code></li> | ||
<li><code>s</code> consists of 0's and 1's only.</li> | ||
<li><code>1 <= k <= 20</code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Bit Manipulation](../../tag/bit-manipulation/README.md)] | ||
[[String](../../tag/string/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
We need only to check all sub-strings of length k. | ||
</details> | ||
|
||
<details> | ||
<summary>Hint 2</summary> | ||
The number of distinct sub-strings should be exactly 2^k. | ||
</details> |
86 changes: 86 additions & 0 deletions
problems/cherry-pickup-ii/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
<!--|@author openset <openset.wang@gmail.com> |--> | ||
<!--|@link https://github.com/openset |--> | ||
<!--|@home https://github.com/openset/leetcode |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
[< 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") | ||
|
||
<p>Given a <code>rows x cols</code> matrix <code>grid</code> representing a field of cherries. Each cell in <code>grid</code> represents the number of cherries that you can collect.</p> | ||
|
||
<p>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.</p> | ||
|
||
<p>Return the maximum number of cherries collection using both robots by following the rules below:</p> | ||
|
||
<ul> | ||
<li>From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1).</li> | ||
<li>When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0).</li> | ||
<li>When both robots stay on the same cell, only one of them takes the cherries.</li> | ||
<li>Both robots cannot move outside of the grid at any moment.</li> | ||
<li>Both robots should reach the bottom row in the <code>grid</code>.</li> | ||
</ul> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/29/sample_1_1802.png" style="width: 139px; height: 182px;" /></strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] | ||
<strong>Output:</strong> 24 | ||
<strong>Explanation:</strong> 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. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/23/sample_2_1802.png" style="width: 284px; height: 257px;" /></strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> 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]] | ||
<strong>Output:</strong> 28 | ||
<strong>Explanation:</strong> 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. | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]] | ||
<strong>Output:</strong> 22 | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> grid = [[1,1],[1,1]] | ||
<strong>Output:</strong> 4 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>rows == grid.length</code></li> | ||
<li><code>cols == grid[i].length</code></li> | ||
<li><code>2 <= rows, cols <= 70</code></li> | ||
<li><code>0 <= grid[i][j] <= 100 </code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Dynamic Programming](../../tag/dynamic-programming/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
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. | ||
</details> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
problems/course-schedule-iv/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
<!--|@author openset <openset.wang@gmail.com> |--> | ||
<!--|@link https://github.com/openset |--> | ||
<!--|@home https://github.com/openset/leetcode |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
[< 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") | ||
|
||
<p>There are a total of <code>n</code> courses you have to take, labeled from <code>0</code> to <code>n-1</code>.</p> | ||
|
||
<p>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: <code>[1,0]</code></p> | ||
|
||
<p>Given the total number of courses <code>n</code>, a list of direct <code>prerequisite</code> <strong>pairs</strong> and a list of <code>queries</code> <strong>pairs</strong>.</p> | ||
|
||
<p>You should answer for each <code>queries[i]</code> whether the course <code>queries[i][0]</code> is a prerequisite of the course <code>queries[i][1]</code> or not.</p> | ||
|
||
<p>Return <em>a list of boolean</em>, the answers to the given <code>queries</code>.</p> | ||
|
||
<p>Please note that if course <strong>a</strong> is a prerequisite of course <strong>b</strong> and course <strong>b</strong> is a prerequisite of course <strong>c</strong>, then, course <strong>a</strong> is a prerequisite of course <strong>c</strong>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/17/graph.png" style="width: 300px; height: 300px;" /> | ||
<pre> | ||
<strong>Input:</strong> n = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]] | ||
<strong>Output:</strong> [false,true] | ||
<strong>Explanation:</strong> course 0 is not a prerequisite of course 1 but the opposite is true. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 2, prerequisites = [], queries = [[1,0],[0,1]] | ||
<strong>Output:</strong> [false,false] | ||
<strong>Explanation:</strong> There are no prerequisites and each course is independent. | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/17/graph-1.png" style="width: 300px; height: 300px;" /> | ||
<pre> | ||
<strong>Input:</strong> n = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]] | ||
<strong>Output:</strong> [true,true] | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 3, prerequisites = [[1,0],[2,0]], queries = [[0,1],[2,0]] | ||
<strong>Output:</strong> [false,true] | ||
</pre> | ||
|
||
<p><strong>Example 5:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 5, prerequisites = [[0,1],[1,2],[2,3],[3,4]], queries = [[0,4],[4,0],[1,3],[3,0]] | ||
<strong>Output:</strong> [true,false,true,false] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>2 <= n <= 100</code></li> | ||
<li><code>0 <= prerequisite.length <= (n * (n - 1) / 2)</code></li> | ||
<li><code>0 <= prerequisite[i][0], prerequisite[i][1] < n</code></li> | ||
<li><code>prerequisite[i][0] != prerequisite[i][1]</code></li> | ||
<li>The prerequisites graph has no cycles.</li> | ||
<li>The prerequisites graph has no repeated edges.</li> | ||
<li><code>1 <= queries.length <= 10^4</code></li> | ||
<li><code>queries[i][0] != queries[i][1]</code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Graph](../../tag/graph/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
Imagine if the courses are nodes of a graph. We need to build an array isReachable[i][j]. | ||
</details> | ||
|
||
<details> | ||
<summary>Hint 2</summary> | ||
Start a bfs from each course i and assign for each course j you visit isReachable[i][j] = True. | ||
</details> | ||
|
||
<details> | ||
<summary>Hint 3</summary> | ||
Answer the queries from the isReachable array. | ||
</details> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.