Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
awesee merged 1 commit into master from develop
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ LeetCode Problems' Solutions

| # | Title | Solution | Difficulty |
| :-: | - | - | :-: |
| <span id="1467">1467</span> | [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 |
| <span id="1466">1466</span> | [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 |
| <span id="1465">1465</span> | [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 |
| <span id="1464">1464</span> | [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 |
| <span id="1463">1463</span> | [Cherry Pickup II](https://leetcode.com/problems/cherry-pickup-ii "摘樱桃 II") | [Go](problems/cherry-pickup-ii) | Hard |
| <span id="1462">1462</span> | [Course Schedule IV](https://leetcode.com/problems/course-schedule-iv "课程安排 IV") | [Go](problems/course-schedule-iv) | Medium |
| <span id="1461">1461</span> | [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 |
| <span id="1460">1460</span> | [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 |
| <span id="1459">1459</span> | [Rectangles Area](https://leetcode.com/problems/rectangles-area) 🔒 | [MySQL](problems/rectangles-area) | Medium |
| <span id="1458">1458</span> | [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 |
| <span id="1457">1457</span> | [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 |
| <span id="1456">1456</span> | [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 |
Expand All @@ -80,7 +89,7 @@ LeetCode Problems' Solutions
| <span id="1443">1443</span> | [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 |
| <span id="1442">1442</span> | [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 |
| <span id="1441">1441</span> | [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 |
| <span id="1440">1440</span> | [Evaluate Boolean Expression](https://leetcode.com/problems/evaluate-boolean-expression) 🔒 | [MySQL](problems/evaluate-boolean-expression) | Medium |
| <span id="1440">1440</span> | [Evaluate Boolean Expression](https://leetcode.com/problems/evaluate-boolean-expression "计算布尔表达式的值") 🔒 | [MySQL](problems/evaluate-boolean-expression) | Medium |
| <span id="1439">1439</span> | [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 |
| <span id="1438">1438</span> | [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 |
| <span id="1437">1437</span> | [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 |
Expand Down
18 changes: 14 additions & 4 deletions problems/3sum-closest/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@

<p>Given an array <code>nums</code> of <em>n</em> integers and an integer <code>target</code>, find three integers in <code>nums</code>&nbsp;such that the sum is closest to&nbsp;<code>target</code>. Return the sum of the three integers. You may assume that each input would have exactly one solution.</p>

<p><strong>Example:</strong></p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre>
Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
<strong>Input:</strong> nums = [-1,2,1,-4], target = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>3 &lt;= nums.length &lt;= 10^3</code></li>
<li><code>-10^3&nbsp;&lt;= nums[i]&nbsp;&lt;= 10^3</code></li>
<li><code>-10^4&nbsp;&lt;= target&nbsp;&lt;= 10^4</code></li>
</ul>

### Related Topics
[[Array](../../tag/array/README.md)]
[[Two Pointers](../../tag/two-pointers/README.md)]
Expand Down
View file Open in desktop
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>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;00110110&quot;, k = 2
<strong>Output:</strong> true
<strong>Explanation:</strong> The binary codes of length 2 are &quot;00&quot;, &quot;01&quot;, &quot;10&quot; and &quot;11&quot;. 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 = &quot;00110&quot;, k = 2
<strong>Output:</strong> true
</pre>

<p><strong>Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;0110&quot;, k = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> The binary codes of length 1 are &quot;0&quot; and &quot;1&quot;, it is clear that both exist as a substring.
</pre>

<p><strong>Example 4:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;0110&quot;, k = 2
<strong>Output:</strong> false
<strong>Explanation:</strong> The binary code &quot;00&quot; is of length 2 and doesn&#39;t exist in the array.
</pre>

<p><strong>Example 5:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;0000000001011100&quot;, k = 4
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 5 * 10^5</code></li>
<li><code>s</code> consists of 0&#39;s and 1&#39;s only.</li>
<li><code>1 &lt;= k &lt;= 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
View file Open in desktop
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.&nbsp;Each cell in&nbsp;<code>grid</code>&nbsp;represents the number of cherries that you can collect.</p>

<p>You have two&nbsp;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&nbsp; 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&nbsp;any moment.</li>
<li>Both robots should reach the bottom row in the <code>grid</code>.</li>
</ul>

<p>&nbsp;</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>&nbsp;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>&nbsp;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>&nbsp;</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 &lt;= rows, cols &lt;= 70</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 100&nbsp;</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>
6 changes: 3 additions & 3 deletions problems/cinema-seat-allocation/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

<p>A cinema&nbsp;has <code>n</code>&nbsp;rows of seats, numbered from 1 to <code>n</code>&nbsp;and there are ten&nbsp;seats in each row, labelled from 1&nbsp;to 10&nbsp;as shown in the figure above.</p>

<p>Given the array <code>reservedSeats</code> containing the numbers of seats already reserved, for example, <code>reservedSeats[i]=[3,8]</code>&nbsp;means the seat located in row <strong>3</strong> and labelled with <b>8</b>&nbsp;is already reserved.&nbsp;</p>
<p>Given the array <code>reservedSeats</code> containing the numbers of seats already reserved, for example, <code>reservedSeats[i] = [3,8]</code>&nbsp;means the seat located in row <strong>3</strong> and labelled with <b>8</b>&nbsp;is already reserved.</p>

<p><em>Return the maximum number of four-person families you can allocate on the cinema&nbsp;seats.</em> A four-person family occupies fours seats <strong>in one row</strong>, that are <strong>next to each other</strong>. Seats across an aisle (such as [3,3]&nbsp;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, <strong>exactly two people</strong> have to sit on each side of the aisle.</p>
<p><em>Return the maximum number of four-person groups&nbsp;you can assign on the cinema&nbsp;seats.</em> A four-person group&nbsp;occupies four&nbsp;adjacent seats <strong>in one single row</strong>. Seats across an aisle (such as [3,3]&nbsp;and [3,4]) are not considered to be adjacent, but there is an exceptional case&nbsp;on which an aisle split&nbsp;a four-person group, in that case, the aisle split&nbsp;a four-person group in the middle,&nbsp;which means to have two people on each side.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
Expand All @@ -27,7 +27,7 @@
<pre>
<strong>Input:</strong> n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> 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.&nbsp;
<strong>Explanation:</strong> 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.
</pre>

<p><strong>Example 2:</strong></p>
Expand Down
95 changes: 95 additions & 0 deletions problems/course-schedule-iv/README.md
View file Open in desktop
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>,&nbsp;a list of direct&nbsp;<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&nbsp;prerequisite of the course&nbsp;<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&nbsp;of course <strong>c</strong>, then, course <strong>a</strong> is a&nbsp;prerequisite of course <strong>c</strong>.</p>

<p>&nbsp;</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>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= n &lt;= 100</code></li>
<li><code>0 &lt;= prerequisite.length &lt;= (n * (n - 1) / 2)</code></li>
<li><code>0 &lt;= prerequisite[i][0], prerequisite[i][1] &lt; 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 &lt;= queries.length &lt;= 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>
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Return&nbsp;<code>True</code>&nbsp;if its possible<strong>&nbsp;</strong>otherwi
<li><code>1 &lt;= nums[i] &lt;= 10^9</code></li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>
<strong>Note:</strong> This question is the same as&nbsp;846:&nbsp;<a href="https://leetcode.com/problems/hand-of-straights/">https://leetcode.com/problems/hand-of-straights/</a>

### Related Topics
[[Greedy](../../tag/greedy/README.md)]
Expand Down
Loading

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