Skip to content

Commit aad60c4

Browse files
author
Shuo
committed
A: new
1 parent dc078f4 commit aad60c4

File tree

43 files changed

+712
-278
lines changed

Some content is hidden

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

43 files changed

+712
-278
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ LeetCode Problems' Solutions
7070

7171
| # | Title | Solution | Difficulty |
7272
| :-: | - | - | :-: |
73+
| <span id="1649">1649</span> | [Create Sorted Array through Instructions](https://leetcode.com/problems/create-sorted-array-through-instructions "通过指令创建有序数组") | [Go](problems/create-sorted-array-through-instructions) | Hard |
74+
| <span id="1648">1648</span> | [Sell Diminishing-Valued Colored Balls](https://leetcode.com/problems/sell-diminishing-valued-colored-balls "销售价值减少的颜色球") | [Go](problems/sell-diminishing-valued-colored-balls) | Medium |
75+
| <span id="1647">1647</span> | [Minimum Deletions to Make Character Frequencies Unique](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique "字符频次唯一的最小删除次数") | [Go](problems/minimum-deletions-to-make-character-frequencies-unique) | Medium |
76+
| <span id="1646">1646</span> | [Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array "获取生成数组中的最大值") | [Go](problems/get-maximum-in-generated-array) | Easy |
77+
| <span id="1645">1645</span> | [Hopper Company Queries II](https://leetcode.com/problems/hopper-company-queries-ii) 🔒 | [MySQL](problems/hopper-company-queries-ii) | Hard |
78+
| <span id="1644">1644</span> | [Lowest Common Ancestor of a Binary Tree II](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii) 🔒 | [Go](problems/lowest-common-ancestor-of-a-binary-tree-ii) | Medium |
7379
| <span id="1643">1643</span> | [Kth Smallest Instructions](https://leetcode.com/problems/kth-smallest-instructions "第 K 条最小指令") | [Go](problems/kth-smallest-instructions) | Hard |
7480
| <span id="1642">1642</span> | [Furthest Building You Can Reach](https://leetcode.com/problems/furthest-building-you-can-reach "可以到达的最远建筑") | [Go](problems/furthest-building-you-can-reach) | Medium |
7581
| <span id="1641">1641</span> | [Count Sorted Vowel Strings](https://leetcode.com/problems/count-sorted-vowel-strings "统计字典序元音字符串的数目") | [Go](problems/count-sorted-vowel-strings) | Medium |

Diff for: problems/best-time-to-buy-and-sell-stock-iv/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
<ul>
4141
<li><code>0 &lt;= k &lt;= 10<sup>9</sup></code></li>
42-
<li><code>0 &lt;= prices.length &lt;= 10<sup>4</sup></code></li>
42+
<li><code>0 &lt;= prices.length &lt;= 1000</code></li>
4343
<li><code>0 &lt;= prices[i] &lt;= 1000</code></li>
4444
</ul>
4545

Diff for: problems/binary-tree-tilt/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
6060

6161
### Related Topics
6262
[[Tree](../../tag/tree/README.md)]
63+
[[Depth-first Search](../../tag/depth-first-search/README.md)]
64+
[[Recursion](../../tag/recursion/README.md)]
6365

6466
### Hints
6567
<details>

Diff for: problems/bulb-switcher/README.md

+34-11
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,45 @@
1111

1212
## [319. Bulb Switcher (Medium)](https://leetcode.com/problems/bulb-switcher "灯泡开关")
1313

14-
<p>There are <i>n</i> bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it&#39;s off or turning off if it&#39;s on). For the <i>i</i>-th round, you toggle every <i>i</i> bulb. For the <i>n</i>-th round, you only toggle the last bulb. Find how many bulbs are on after <i>n</i> rounds.</p>
14+
<p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then&nbsp;you turn off every second bulb.</p>
1515

16-
<p><b>Example:</b></p>
16+
<p>On the third round, you toggle every third bulb (turning on if it&#39;s off or turning off if it&#39;s on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</code> bulb. For the <code>n<sup>th</sup></code> round, you only toggle the last bulb.</p>
17+
18+
<p>Return <em>the number of bulbs that are on after <code>n</code> rounds</em>.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Example 1:</strong></p>
22+
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bulb.jpg" style="width: 421px; height: 321px;" />
23+
<pre>
24+
<strong>Input:</strong> n = 3
25+
<strong>Output:</strong> 1
26+
<strong>Explanation:</strong> At first, the three bulbs are [off, off, off].
27+
After the first round, the three bulbs are [on, on, on].
28+
After the second round, the three bulbs are [on, off, on].
29+
After the third round, the three bulbs are [on, off, off].
30+
So you should return 1 because there is only one bulb is on.</pre>
31+
32+
<p><strong>Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> n = 0
36+
<strong>Output:</strong> 0
37+
</pre>
38+
39+
<p><strong>Example 3:</strong></p>
1740

1841
<pre>
19-
<strong>Input: </strong>3
20-
<strong>Output:</strong> 1
21-
<strong>Explanation:</strong>
22-
At first, the three bulbs are <b>[off, off, off]</b>.
23-
After first round, the three bulbs are <b>[on, on, on]</b>.
24-
After second round, the three bulbs are <b>[on, off, on]</b>.
25-
After third round, the three bulbs are <b>[on, off, off]</b>.
26-
27-
So you should return 1, because there is only one bulb is on.
42+
<strong>Input:</strong> n = 1
43+
<strong>Output:</strong> 1
2844
</pre>
2945

46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>
51+
</ul>
52+
3053
### Related Topics
3154
[[Brainteaser](../../tag/brainteaser/README.md)]
3255
[[Math](../../tag/math/README.md)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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](../sell-diminishing-valued-colored-balls "Sell Diminishing-Valued Colored Balls")
9+
                
10+
Next >
11+
12+
## [1649. Create Sorted Array through Instructions (Hard)](https://leetcode.com/problems/create-sorted-array-through-instructions "通过指令创建有序数组")
13+
14+
<p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
15+
16+
<ul>
17+
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
18+
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
19+
</ul>
20+
21+
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
22+
23+
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> instructions = [1,5,6,2]
30+
<strong>Output:</strong> 1
31+
<strong>Explanation:</strong> Begin with nums = [].
32+
Insert 1 with cost min(0, 0) = 0, now nums = [1].
33+
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
34+
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
35+
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
36+
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
37+
38+
<p><strong>Example 2:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
42+
<strong>Output:</strong> 3
43+
<strong>Explanation:</strong> Begin with nums = [].
44+
Insert 1 with cost min(0, 0) = 0, now nums = [1].
45+
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
46+
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
47+
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
48+
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
49+
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
50+
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
51+
</pre>
52+
53+
<p><strong>Example 3:</strong></p>
54+
55+
<pre>
56+
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
57+
<strong>Output:</strong> 4
58+
<strong>Explanation:</strong> Begin with nums = [].
59+
Insert 1 with cost min(0, 0) = 0, now nums = [1].
60+
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
61+
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
62+
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
63+
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
64+
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
65+
​​​​​​​Insert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
66+
​​​​​​​Insert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
67+
​​​​​​​Insert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
68+
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
69+
</pre>
70+
71+
<p>&nbsp;</p>
72+
<p><strong>Constraints:</strong></p>
73+
74+
<ul>
75+
<li><code>1 &lt;= instructions.length &lt;= 10<sup>5</sup></code></li>
76+
<li><code>1 &lt;= instructions[i] &lt;= 10<sup>5</sup></code></li>
77+
</ul>
78+
79+
### Related Topics
80+
[[Binary Indexed Tree](../../tag/binary-indexed-tree/README.md)]
81+
[[Segment Tree](../../tag/segment-tree/README.md)]
82+
[[Ordered Map](../../tag/ordered-map/README.md)]
83+
84+
### Hints
85+
<details>
86+
<summary>Hint 1</summary>
87+
This problem is closely related to finding the number of inversions in an array
88+
</details>
89+
90+
<details>
91+
<summary>Hint 2</summary>
92+
if i know the position in which i will insert the i-th element in I can find the minimum cost to insert it
93+
</details>

Diff for: problems/flower-planting-with-no-adjacent/README.md

+27-11
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,43 @@
99

1010
[Next >](../partition-array-for-maximum-sum "Partition Array for Maximum Sum")
1111

12-
## [1042. Flower Planting With No Adjacent (Easy)](https://leetcode.com/problems/flower-planting-with-no-adjacent "不邻接植花")
12+
## [1042. Flower Planting With No Adjacent (Medium)](https://leetcode.com/problems/flower-planting-with-no-adjacent "不邻接植花")
1313

14-
<p>You have <code>n</code> gardens, labeled from&nbsp;<code>1</code> to <code>n</code>, and an array <code>paths</code> where&nbsp;<code>paths[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> describes the existence of a bidirectional path from garden <code>x<sub>i</sub></code> to garden <code>y<sub>i</sub></code>. In each garden, you want to plant one of 4 types of flowers.</p>
14+
<p>You have <code>n</code> gardens, labeled from <code>1</code> to <code>n</code>, and an array <code>paths</code> where <code>paths[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> describes a bidirectional path between garden <code>x<sub>i</sub></code> to garden <code>y<sub>i</sub></code>. In each garden, you want to plant one of 4 types of flowers.</p>
1515

16-
<p>There is no garden that has more than three paths coming into or leaving it.</p>
16+
<p>All gardens have <strong>at most 3</strong> paths coming into or leaving it.</p>
1717

18-
<p>Your task is to choose a flower type for each garden such that,&nbsp;for any two gardens connected by a path, they have different types of flowers.</p>
18+
<p>Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.</p>
1919

20-
<p>Return <strong>any</strong> such a choice as an array <code>answer</code>, where&nbsp;<code>answer[i]</code> is the type of flower&nbsp;planted in the <code>(i+1)<sup>th</sup></code> garden.&nbsp; The flower types are denoted&nbsp;<font face="monospace">1</font>, <font face="monospace">2</font>, <font face="monospace">3</font>, or <font face="monospace">4</font>.&nbsp; It is guaranteed an answer exists.</p>
20+
<p>Return <em><strong>any</strong> such a choice as an array </em><code>answer</code><em>, where </em><code>answer[i]</code><em> is the type of flower planted in the </em><code>(i+1)<sup>th</sup></code><em> garden. The flower types are denoted </em><code>1</code><em>, </em><code>2</code><em>, </em><code>3</code><em>, or </em><code>4</code><em>. It is guaranteed an answer exists.</em></p>
2121

2222
<p>&nbsp;</p>
2323
<p><strong>Example 1:</strong></p>
24-
<pre><strong>Input:</strong> n = 3, paths = [[1,2],[2,3],[3,1]]
24+
25+
<pre>
26+
<strong>Input:</strong> n = 3, paths = [[1,2],[2,3],[3,1]]
2527
<strong>Output:</strong> [1,2,3]
26-
</pre><p><strong>Example 2:</strong></p>
27-
<pre><strong>Input:</strong> n = 4, paths = [[1,2],[3,4]]
28+
<strong>Explanation:</strong>
29+
Gardens 1 and 2 have different types.
30+
Gardens 2 and 3 have different types.
31+
Gardens 3 and 1 have different types.
32+
Hence, [1,2,3] is a valid answer. Other valid answers include [1,2,4], [1,4,2], and [3,2,1].
33+
</pre>
34+
35+
<p><strong>Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> n = 4, paths = [[1,2],[3,4]]
2839
<strong>Output:</strong> [1,2,1,2]
29-
</pre><p><strong>Example 3:</strong></p>
30-
<pre><strong>Input:</strong> n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
40+
</pre>
41+
42+
<p><strong>Example 3:</strong></p>
43+
44+
<pre>
45+
<strong>Input:</strong> n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
3146
<strong>Output:</strong> [1,2,3,4]
3247
</pre>
48+
3349
<p>&nbsp;</p>
3450
<p><strong>Constraints:</strong></p>
3551

@@ -39,7 +55,7 @@
3955
<li><code>paths[i].length == 2</code></li>
4056
<li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= n</code></li>
4157
<li><code>x<sub>i</sub> != y<sub>i</sub></code></li>
42-
<li>No garden has <strong>four or more</strong> paths coming into or leaving it.</li>
58+
<li>Every garden has <strong>at most 3</strong> paths coming into or leaving it.</li>
4359
</ul>
4460

4561
### Related Topics

Diff for: problems/get-maximum-in-generated-array/README.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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](../hopper-company-queries-ii "Hopper Company Queries II")
9+
                
10+
[Next >](../minimum-deletions-to-make-character-frequencies-unique "Minimum Deletions to Make Character Frequencies Unique")
11+
12+
## [1646. Get Maximum in Generated Array (Easy)](https://leetcode.com/problems/get-maximum-in-generated-array "获取生成数组中的最大值")
13+
14+
<p>You are given an integer <code>n</code>. An array <code>nums</code> of length <code>n + 1</code> is generated in the following way:</p>
15+
16+
<ul>
17+
<li><code>nums[0] = 0</code></li>
18+
<li><code>nums[1] = 1</code></li>
19+
<li><code>nums[2 * i] = nums[i]</code> when <code>2 &lt;= 2 * i &lt;= n</code></li>
20+
<li><code>nums[2 * i + 1] = nums[i] + nums[i + 1]</code> when <code>2 &lt;= 2 * i + 1 &lt;= n</code></li>
21+
</ul>
22+
23+
<p>Return<strong> </strong><em>the <strong>maximum</strong> integer in the array </em><code>nums</code>​​​.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> n = 7
30+
<strong>Output:</strong> 3
31+
<strong>Explanation:</strong> According to the given rules:
32+
nums[0] = 0
33+
nums[1] = 1
34+
nums[(1 * 2) = 2] = nums[1] = 1
35+
nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2
36+
nums[(2 * 2) = 4] = nums[2] = 1
37+
nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3
38+
nums[(3 * 2) = 6] = nums[3] = 2
39+
nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3
40+
Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3.
41+
</pre>
42+
43+
<p><strong>Example 2:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong> n = 2
47+
<strong>Output:</strong> 1
48+
<strong>Explanation:</strong> According to the given rules, the maximum between nums[0], nums[1], and nums[2] is 1.
49+
</pre>
50+
51+
<p><strong>Example 3:</strong></p>
52+
53+
<pre>
54+
<strong>Input:</strong> n = 3
55+
<strong>Output:</strong> 2
56+
<strong>Explanation:</strong> According to the given rules, the maximum between nums[0], nums[1], nums[2], and nums[3] is 2.
57+
</pre>
58+
59+
<p>&nbsp;</p>
60+
<p><strong>Constraints:</strong></p>
61+
62+
<ul>
63+
<li><code>0 &lt;= n &lt;= 100</code></li>
64+
</ul>
65+
66+
### Related Topics
67+
[[Array](../../tag/array/README.md)]
68+
69+
### Hints
70+
<details>
71+
<summary>Hint 1</summary>
72+
Try generating the array.
73+
</details>
74+
75+
<details>
76+
<summary>Hint 2</summary>
77+
Make sure not to fall in the base case of 0.
78+
</details>

Diff for: problems/hopper-company-queries-ii/README.md

+14
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](../lowest-common-ancestor-of-a-binary-tree-ii "Lowest Common Ancestor of a Binary Tree II")
9+
                
10+
[Next >](../get-maximum-in-generated-array "Get Maximum in Generated Array")
11+
12+
## [1645. Hopper Company Queries II (Hard)](https://leetcode.com/problems/hopper-company-queries-ii "")
13+
14+

0 commit comments

Comments
 (0)