Skip to content

Commit 5dc31a3

Browse files
author
Shuo
committed
A: new
1 parent b7fdc4b commit 5dc31a3

File tree

45 files changed

+1205
-294
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1205
-294
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ LeetCode Problems' Solutions
7070

7171
| # | Title | Solution | Difficulty |
7272
| :-: | - | - | :-: |
73+
| <span id="1563">1563</span> | [Stone Game V](https://leetcode.com/problems/stone-game-v "石子游戏 V") | [Go](problems/stone-game-v) | Hard |
74+
| <span id="1562">1562</span> | [Find Latest Group of Size M](https://leetcode.com/problems/find-latest-group-of-size-m "查找大小为 M 的最新分组") | [Go](problems/find-latest-group-of-size-m) | Medium |
75+
| <span id="1561">1561</span> | [Maximum Number of Coins You Can Get](https://leetcode.com/problems/maximum-number-of-coins-you-can-get "你可以获得的最大硬币数目") | [Go](problems/maximum-number-of-coins-you-can-get) | Medium |
76+
| <span id="1560">1560</span> | [Most Visited Sector in a Circular Track](https://leetcode.com/problems/most-visited-sector-in-a-circular-track "圆形赛道上经过次数最多的扇区") | [Go](problems/most-visited-sector-in-a-circular-track) | Easy |
77+
| <span id="1559">1559</span> | [Detect Cycles in 2D Grid](https://leetcode.com/problems/detect-cycles-in-2d-grid "二维网格图中探测环") | [Go](problems/detect-cycles-in-2d-grid) | Hard |
78+
| <span id="1558">1558</span> | [Minimum Numbers of Function Calls to Make Target Array](https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array "得到目标数组的最少函数调用次数") | [Go](problems/minimum-numbers-of-function-calls-to-make-target-array) | Medium |
79+
| <span id="1557">1557</span> | [Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes "可以到达所有点的最少点数目") | [Go](problems/minimum-number-of-vertices-to-reach-all-nodes) | Medium |
80+
| <span id="1556">1556</span> | [Thousand Separator](https://leetcode.com/problems/thousand-separator "千位分隔数") | [Go](problems/thousand-separator) | Easy |
81+
| <span id="1555">1555</span> | [Bank Account Summary](https://leetcode.com/problems/bank-account-summary) 🔒 | [MySQL](problems/bank-account-summary) | Medium |
82+
| <span id="1554">1554</span> | [Strings Differ by One Character](https://leetcode.com/problems/strings-differ-by-one-character) 🔒 | [Go](problems/strings-differ-by-one-character) | Medium |
7383
| <span id="1553">1553</span> | [Minimum Number of Days to Eat N Oranges](https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges "吃掉 N 个橘子的最少天数") | [Go](problems/minimum-number-of-days-to-eat-n-oranges) | Hard |
7484
| <span id="1552">1552</span> | [Magnetic Force Between Two Balls](https://leetcode.com/problems/magnetic-force-between-two-balls "两球之间的磁力") | [Go](problems/magnetic-force-between-two-balls) | Medium |
7585
| <span id="1551">1551</span> | [Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal "使数组中所有元素相等的最小操作数") | [Go](problems/minimum-operations-to-make-array-equal) | Medium |

problems/132-pattern/README.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,43 @@
1111

1212
## [456. 132 Pattern (Medium)](https://leetcode.com/problems/132-pattern "132模式")
1313

14-
<p>
15-
Given a sequence of n integers a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>, a 132 pattern is a subsequence a<sub><b>i</b></sub>, a<sub><b>j</b></sub>, a<sub><b>k</b></sub> such
16-
that <b>i</b> < <b>j</b> < <b>k</b> and a<sub><b>i</b></sub> < a<sub><b>k</b></sub> < a<sub><b>j</b></sub>. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.</p>
14+
<p>Given an array&nbsp;of <code>n</code> integers <code>nums</code>, a <strong>132 pattern</strong> is a subsequence of three integers <code>nums[i]</code>, <code>nums[j]</code> and <code>nums[k]</code> such that <code>i &lt; j &lt; k</code> and <code>nums[i] &lt; nums[k] &lt; nums[j]</code>.</p>
1715

18-
<p><b>Note:</b> n will be less than 15,000.</p>
16+
<p>Return <em><code>true</code> if there is a <strong>132 pattern</strong> in <code>nums</code>, otherwise return <code>false</code>.</em></p>
1917

20-
<p><b>Example 1:</b><br />
21-
<pre>
22-
<b>Input:</b> [1, 2, 3, 4]
23-
24-
<b>Output:</b> False
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
2520

26-
<b>Explanation:</b> There is no 132 pattern in the sequence.
27-
</pre>
28-
</p>
29-
30-
<p><b>Example 2:</b><br />
3121
<pre>
32-
<b>Input:</b> [3, 1, 4, 2]
22+
<strong>Input:</strong> nums = [1,2,3,4]
23+
<strong>Output:</strong> false
24+
<strong>Explanation:</strong> There is no 132 pattern in the sequence.
25+
</pre>
3326

34-
<b>Output:</b> True
27+
<p><strong>Example 2:</strong></p>
3528

36-
<b>Explanation:</b> There is a 132 pattern in the sequence: [1, 4, 2].
29+
<pre>
30+
<strong>Input:</strong> nums = [3,1,4,2]
31+
<strong>Output:</strong> true
32+
<strong>Explanation:</strong> There is a 132 pattern in the sequence: [1, 4, 2].
3733
</pre>
38-
</p>
3934

40-
<p><b>Example 3:</b><br />
35+
<p><strong>Example 3:</strong></p>
36+
4137
<pre>
42-
<b>Input:</b> [-1, 3, 2, 0]
38+
<strong>Input:</strong> nums = [-1,3,2,0]
39+
<strong>Output:</strong> true
40+
<strong>Explanation:</strong> There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
41+
</pre>
4342

44-
<b>Output:</b> True
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
4545

46-
<b>Explanation:</b> There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
47-
</pre>
48-
</p>
46+
<ul>
47+
<li><code>n == nums.length</code></li>
48+
<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>
49+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
50+
</ul>
4951

5052
### Related Topics
5153
[[Stack](../../tag/stack/README.md)]

problems/3sum-with-multiplicity/README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313

1414
<p>Given an integer array <code>A</code>, and an integer <code>target</code>, return the number of&nbsp;tuples&nbsp;<code>i, j, k</code>&nbsp; such that <code>i &lt; j &lt; k</code> and&nbsp;<code>A[i] + A[j] + A[k] == target</code>.</p>
1515

16-
<p><strong>As the answer can be very large, return it modulo&nbsp;<code>10^9 + 7</code></strong>.</p>
16+
<p>As the answer can be very large, return it <strong>modulo</strong>&nbsp;<code>10<sup>9</sup> + 7</code>.</p>
1717

1818
<p>&nbsp;</p>
19-
2019
<p><strong>Example 1:</strong></p>
2120

2221
<pre>
23-
<strong>Input: </strong>A = <span id="example-input-1-1">[1,1,2,2,3,3,4,4,5,5]</span>, target = <span id="example-input-1-2">8</span>
24-
<strong>Output: </strong><span id="example-output-1">20</span>
22+
<strong>Input:</strong> A = [1,1,2,2,3,3,4,4,5,5], target = 8
23+
<strong>Output:</strong> 20
2524
<strong>Explanation: </strong>
2625
Enumerating by the values (A[i], A[j], A[k]):
2726
(1, 2, 5) occurs 8 times;
@@ -30,28 +29,25 @@ Enumerating by the values (A[i], A[j], A[k]):
3029
(2, 3, 3) occurs 2 times.
3130
</pre>
3231

33-
<div>
3432
<p><strong>Example 2:</strong></p>
3533

3634
<pre>
37-
<strong>Input: </strong>A = <span id="example-input-2-1">[1,1,2,2,2,2]</span>, target = <span id="example-input-2-2">5</span>
38-
<strong>Output: </strong><span id="example-output-2">12</span>
35+
<strong>Input:</strong> A = [1,1,2,2,2,2], target = 5
36+
<strong>Output:</strong> 12
3937
<strong>Explanation: </strong>
4038
A[i] = 1, A[j] = A[k] = 2 occurs 12 times:
4139
We choose one 1 from [1,1] in 2 ways,
4240
and two 2s from [2,2,2,2] in 6 ways.
4341
</pre>
4442

4543
<p>&nbsp;</p>
46-
</div>
47-
48-
<p><strong>Note:</strong></p>
44+
<p><strong>Constraints:</strong></p>
4945

50-
<ol>
46+
<ul>
5147
<li><code>3 &lt;= A.length &lt;= 3000</code></li>
5248
<li><code>0 &lt;= A[i] &lt;= 100</code></li>
5349
<li><code>0 &lt;= target &lt;= 300</code></li>
54-
</ol>
50+
</ul>
5551

5652
### Related Topics
5753
[[Two Pointers](../../tag/two-pointers/README.md)]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <openset.wang@gmail.com> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../strings-differ-by-one-character "Strings Differ by One Character")
9+
                
10+
[Next >](../thousand-separator "Thousand Separator")
11+
12+
## [1555. Bank Account Summary (Medium)](https://leetcode.com/problems/bank-account-summary "")
13+
14+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Create table If Not Exists Users (user_id int, user_name varchar(20), credit int)
2+
;
3+
Create table If Not Exists Transaction (trans_id int, paid_by int, paid_to int, amount int, transacted_on date);
4+
Truncate table Users;
5+
insert into Users (user_id, user_name, credit) values ('1', 'Moustafa', '100');
6+
insert into Users (user_id, user_name, credit) values ('2', 'Jonathan', '200');
7+
insert into Users (user_id, user_name, credit) values ('3', 'Winston', '10000');
8+
insert into Users (user_id, user_name, credit) values ('4', 'Luis', '800');
9+
Truncate table Transaction;
10+
insert into Transaction (trans_id, paid_by, paid_to, amount, transacted_on) values ('1', '1', '3', '400', '2020-08-01');
11+
insert into Transaction (trans_id, paid_by, paid_to, amount, transacted_on) values ('2', '3', '2', '500', '2020-08-02');
12+
insert into Transaction (trans_id, paid_by, paid_to, amount, transacted_on) values ('3', '2', '1', '200', '2020-08-03');

problems/best-time-to-buy-and-sell-stock-iii/README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,46 @@
1717

1818
<p><strong>Note:&nbsp;</strong>You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).</p>
1919

20+
<p>&nbsp;</p>
2021
<p><strong>Example 1:</strong></p>
2122

2223
<pre>
23-
<strong>Input:</strong> [3,3,5,0,0,3,1,4]
24+
<strong>Input:</strong> prices = [3,3,5,0,0,3,1,4]
2425
<strong>Output:</strong> 6
2526
<strong>Explanation:</strong> Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
26-
&nbsp; Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.</pre>
27+
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.</pre>
2728

2829
<p><strong>Example 2:</strong></p>
2930

3031
<pre>
31-
<strong>Input:</strong> [1,2,3,4,5]
32+
<strong>Input:</strong> prices = [1,2,3,4,5]
3233
<strong>Output:</strong> 4
3334
<strong>Explanation:</strong> Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
34-
&nbsp; Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
35-
&nbsp; engaging multiple transactions at the same time. You must sell before buying again.
35+
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
3636
</pre>
3737

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

4040
<pre>
41-
<strong>Input:</strong> [7,6,4,3,1]
41+
<strong>Input:</strong> prices = [7,6,4,3,1]
4242
<strong>Output:</strong> 0
43-
<strong>Explanation:</strong> In this case, no transaction is done, i.e. max profit = 0.</pre>
43+
<strong>Explanation:</strong> In this case, no transaction is done, i.e. max profit = 0.
44+
</pre>
45+
46+
<p><strong>Example 4:</strong></p>
47+
48+
<pre>
49+
<strong>Input:</strong> prices = [1]
50+
<strong>Output:</strong> 0
51+
</pre>
52+
53+
<p>&nbsp;</p>
54+
<p><strong>Constraints:</strong></p>
55+
56+
<ul>
57+
<li><code>1 &lt;=&nbsp;prices.length &lt;= 10<sup>5</sup></code></li>
58+
<li><code>0 &lt;=&nbsp;prices[i] &lt;=&nbsp;10<sup>5</sup></code></li>
59+
</ul>
4460

4561
### Related Topics
4662
[[Array](../../tag/array/README.md)]

problems/convert-sorted-list-to-binary-search-tree/README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,48 @@
1111

1212
## [109. Convert Sorted List to Binary Search Tree (Medium)](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree "有序链表转换二叉搜索树")
1313

14-
<p>Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.</p>
14+
<p>Given the <code>head</code> of a singly linked list where elements are <strong>sorted in ascending order</strong>, convert it to a height balanced BST.</p>
1515

1616
<p>For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of <em>every</em> node never differ by more than 1.</p>
1717

18-
<p><strong>Example:</strong></p>
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 600px; height: 466px;" />
21+
<pre>
22+
<strong>Input:</strong> head = [-10,-3,0,5,9]
23+
<strong>Output:</strong> [0,-3,9,-10,null,5]
24+
<strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
1928

2029
<pre>
21-
Given the sorted linked list: [-10,-3,0,5,9],
30+
<strong>Input:</strong> head = []
31+
<strong>Output:</strong> []
32+
</pre>
2233

23-
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
34+
<p><strong>Example 3:</strong></p>
2435

25-
0
26-
/ \
27-
-3 9
28-
/ /
29-
-10 5
36+
<pre>
37+
<strong>Input:</strong> head = [0]
38+
<strong>Output:</strong> [0]
3039
</pre>
3140

41+
<p><strong>Example 4:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> head = [1,3]
45+
<strong>Output:</strong> [3,1]
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li>The numner of nodes in <code>head</code> is in the range <code>[0, 2 * 10^4]</code>.</li>
53+
<li><code>-10^5 &lt;= Node.val &lt;= 10^5</code></li>
54+
</ul>
55+
3256
### Related Topics
3357
[[Depth-first Search](../../tag/depth-first-search/README.md)]
3458
[[Linked List](../../tag/linked-list/README.md)]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <openset.wang@gmail.com> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../minimum-numbers-of-function-calls-to-make-target-array "Minimum Numbers of Function Calls to Make Target Array")
9+
                
10+
[Next >](../most-visited-sector-in-a-circular-track "Most Visited Sector in a Circular Track")
11+
12+
## [1559. Detect Cycles in 2D Grid (Hard)](https://leetcode.com/problems/detect-cycles-in-2d-grid "二维网格图中探测环")
13+
14+
<p>Given a 2D array of characters&nbsp;<code>grid</code>&nbsp;of size <code>m x n</code>, you need to find if there exists any cycle consisting of the <strong>same value</strong>&nbsp;in&nbsp;<code>grid</code>.</p>
15+
16+
<p>A cycle is a path of <strong>length 4&nbsp;or more</strong>&nbsp;in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the <strong>same value</strong> of the current cell.</p>
17+
18+
<p>Also, you cannot move to the cell that you visited in your last move. For example, the cycle&nbsp;<code>(1, 1) -&gt; (1, 2) -&gt; (1, 1)</code>&nbsp;is invalid because from&nbsp;<code>(1, 2)</code>&nbsp;we visited&nbsp;<code>(1, 1)</code>&nbsp;which was the last visited cell.</p>
19+
20+
<p>Return&nbsp;<code>true</code>&nbsp;if any cycle of the same value exists in&nbsp;<code>grid</code>, otherwise, return&nbsp;<code>false</code>.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
25+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/1.png" style="width: 231px; height: 152px;" /></strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> grid = [[&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;]]
29+
<strong>Output:</strong> true
30+
<strong>Explanation: </strong>There are two valid cycles shown in different colors in the image below:
31+
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/11.png" style="width: 225px; height: 163px;" />
32+
</pre>
33+
34+
<p><strong>Example 2:</strong></p>
35+
36+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/22.png" style="width: 236px; height: 154px;" /></strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> grid = [[&quot;c&quot;,&quot;c&quot;,&quot;c&quot;,&quot;a&quot;],[&quot;c&quot;,&quot;d&quot;,&quot;c&quot;,&quot;c&quot;],[&quot;c&quot;,&quot;c&quot;,&quot;e&quot;,&quot;c&quot;],[&quot;f&quot;,&quot;c&quot;,&quot;c&quot;,&quot;c&quot;]]
40+
<strong>Output:</strong> true
41+
<strong>Explanation: </strong>There is only one valid cycle highlighted in the image below:
42+
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/2.png" style="width: 229px; height: 157px;" />
43+
</pre>
44+
45+
<p><strong>Example 3:</strong></p>
46+
47+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/3.png" style="width: 183px; height: 120px;" /></strong></p>
48+
49+
<pre>
50+
<strong>Input:</strong> grid = [[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;z&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;b&quot;,&quot;a&quot;]]
51+
<strong>Output:</strong> false
52+
</pre>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Constraints:</strong></p>
56+
57+
<ul>
58+
<li><code>m == grid.length</code></li>
59+
<li><code>n == grid[i].length</code></li>
60+
<li><code>1 &lt;= m &lt;= 500</code></li>
61+
<li><code>1 &lt;= n &lt;= 500</code></li>
62+
<li><code>grid</code>&nbsp;consists only of lowercase&nbsp;English letters.</li>
63+
</ul>
64+
65+
### Related Topics
66+
[[Depth-first Search](../../tag/depth-first-search/README.md)]
67+
68+
### Hints
69+
<details>
70+
<summary>Hint 1</summary>
71+
Keep track of the parent (previous position) to avoid considering an invalid path.
72+
</details>
73+
74+
<details>
75+
<summary>Hint 2</summary>
76+
Use DFS or BFS and keep track of visited cells to see if there is a cycle.
77+
</details>

problems/distribute-candies/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
## [575. Distribute Candies (Easy)](https://leetcode.com/problems/distribute-candies "分糖果")
1313

14-
<p>You have <code>n</code>&nbsp;<code>candies</code> of&nbsp;different types, the <code>i<sup>th</sup></code> candy is of type <code>candies[i]</code>.</p>
14+
<p>You have <code>n</code>&nbsp;<code>candies</code>, the <code>i<sup>th</sup></code> candy is of type <code>candies[i]</code>.</p>
1515

1616
<p>You want to distribute the candies equally between a sister and a brother so that each of them gets <code>n / 2</code>&nbsp;candies (<code>n</code> is even). The sister loves to collect different types of candies, so you want to give her the <strong>maximum number of different types</strong> of candies.</p>
1717

0 commit comments

Comments
 (0)