Skip to content

Commit 27bb8ea

Browse files
author
Shuo
committed
A: new
1 parent cccea33 commit 27bb8ea

File tree

17 files changed

+398
-4
lines changed

17 files changed

+398
-4
lines changed

README.md

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

7171
| # | Title | Solution | Difficulty |
7272
| :-: | - | - | :-: |
73+
| <span id="1521">1521</span> | [Find a Value of a Mysterious Function Closest to Target](https://leetcode.com/problems/find-a-value-of-a-mysterious-function-closest-to-target "找到最接近目标值的函数值") | [Go](problems/find-a-value-of-a-mysterious-function-closest-to-target) | Hard |
74+
| <span id="1520">1520</span> | [Maximum Number of Non-Overlapping Substrings](https://leetcode.com/problems/maximum-number-of-non-overlapping-substrings "最多的不重叠子字符串") | [Go](problems/maximum-number-of-non-overlapping-substrings) | Medium |
75+
| <span id="1519">1519</span> | [Number of Nodes in the Sub-Tree With the Same Label](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label "子树中标签相同的节点数") | [Go](problems/number-of-nodes-in-the-sub-tree-with-the-same-label) | Medium |
76+
| <span id="1518">1518</span> | [Water Bottles](https://leetcode.com/problems/water-bottles "换酒问题") | [Go](problems/water-bottles) | Easy |
77+
| <span id="1517">1517</span> | [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails) 🔒 | [MySQL](problems/find-users-with-valid-e-mails) | Easy |
78+
| <span id="1516">1516</span> | [Move Sub-Tree of N-Ary Tree](https://leetcode.com/problems/move-sub-tree-of-n-ary-tree) 🔒 | [Go](problems/move-sub-tree-of-n-ary-tree) | Hard |
7379
| <span id="1515">1515</span> | [Best Position for a Service Centre](https://leetcode.com/problems/best-position-for-a-service-centre "服务中心的最佳位置") | [Go](problems/best-position-for-a-service-centre) | Hard |
7480
| <span id="1514">1514</span> | [Path with Maximum Probability](https://leetcode.com/problems/path-with-maximum-probability "概率最大的路径") | [Go](problems/path-with-maximum-probability) | Medium |
7581
| <span id="1513">1513</span> | [Number of Substrings With Only 1s](https://leetcode.com/problems/number-of-substrings-with-only-1s "仅含 1 的子串数") | [Go](problems/number-of-substrings-with-only-1s) | Medium |
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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](../maximum-number-of-non-overlapping-substrings "Maximum Number of Non-Overlapping Substrings")
9+
                
10+
Next >
11+
12+
## [1521. Find a Value of a Mysterious Function Closest to Target (Hard)](https://leetcode.com/problems/find-a-value-of-a-mysterious-function-closest-to-target "找到最接近目标值的函数值")
13+
14+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/09/change.png" style="width: 635px; height: 312px;" /></p>
15+
16+
<p>Winston was given the above mysterious function <code>func</code>. He has an integer array <code>arr</code> and an integer <code>target</code> and he wants to find the values&nbsp;<code>l</code> and <code>r</code>&nbsp;that make&nbsp;the value <code>|func(arr, l, r) - target|</code> minimum possible.</p>
17+
18+
<p>Return <em>the minimum possible value</em> of <code>|func(arr, l, r) - target|</code>.</p>
19+
20+
<p>Notice that <code>func</code> should be called with the values&nbsp;<code>l</code> and <code>r</code> where <code>0 &lt;= l, r &lt; arr.length</code>.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> arr = [9,12,3,7,15], target = 5
27+
<strong>Output:</strong> 2
28+
<strong>Explanation:</strong> Calling func with all the pairs of [l,r] = [[0,0],[1,1],[2,2],[3,3],[4,4],[0,1],[1,2],[2,3],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[0,4]], Winston got the following results [9,12,3,7,15,8,0,3,7,0,0,3,0,0,0]. The value closest to 5 is 7 and 3, thus the minimum difference is 2.
29+
</pre>
30+
31+
<p><strong>Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> arr = [1000000,1000000,1000000], target = 1
35+
<strong>Output:</strong> 999999
36+
<strong>Explanation:</strong> Winston called the func with all possible values of [l,r] and he always got 1000000, thus the min difference is 999999.
37+
</pre>
38+
39+
<p><strong>Example 3:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> arr = [1,2,4,8,16], target = 0
43+
<strong>Output:</strong> 0
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= arr.length &lt;= 10^5</code></li>
51+
<li><code>1 &lt;= arr[i] &lt;= 10^6</code></li>
52+
<li><code>0 &lt;= target &lt;= 10^7</code></li>
53+
</ul>
54+
55+
### Related Topics
56+
[[Bit Manipulation](../../tag/bit-manipulation/README.md)]
57+
[[Segment Tree](../../tag/segment-tree/README.md)]
58+
[[Binary Search](../../tag/binary-search/README.md)]
59+
60+
### Hints
61+
<details>
62+
<summary>Hint 1</summary>
63+
If the and value of sub-array arr[i...j] is ≥ the and value of the sub-array arr[i...j+1].
64+
</details>
65+
66+
<details>
67+
<summary>Hint 2</summary>
68+
For each index i using binary search or ternary search find the index j where |target - AND(arr[i...j])| is minimum, minimize this value with the global answer.
69+
</details>
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](../move-sub-tree-of-n-ary-tree "Move Sub-Tree of N-Ary Tree")
9+
                
10+
[Next >](../water-bottles "Water Bottles")
11+
12+
## [1517. Find Users With Valid E-Mails (Easy)](https://leetcode.com/problems/find-users-with-valid-e-mails "")
13+
14+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Create table If Not Exists Users (user_id int, name varchar(30), mail varchar(50));
2+
Truncate table Users;
3+
insert into Users (user_id, name, mail) values ('1', 'Winston', 'winston@leetcode.com');
4+
insert into Users (user_id, name, mail) values ('2', 'Jonathan', 'jonathanisgreat');
5+
insert into Users (user_id, name, mail) values ('3', 'Annabelle', 'bella-@leetcode.com');
6+
insert into Users (user_id, name, mail) values ('4', 'Sally', 'sally.come@leetcode.com');
7+
insert into Users (user_id, name, mail) values ('5', 'Marwan', 'quarz#2020@leetcode.com');
8+
insert into Users (user_id, name, mail) values ('6', 'David', 'david69@gmail.com');
9+
insert into Users (user_id, name, mail) values ('7', 'Shapiro', '.shapo@leetcode.com');
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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](../number-of-nodes-in-the-sub-tree-with-the-same-label "Number of Nodes in the Sub-Tree With the Same Label")
9+
                
10+
[Next >](../find-a-value-of-a-mysterious-function-closest-to-target "Find a Value of a Mysterious Function Closest to Target")
11+
12+
## [1520. Maximum Number of Non-Overlapping Substrings (Medium)](https://leetcode.com/problems/maximum-number-of-non-overlapping-substrings "最多的不重叠子字符串")
13+
14+
<p>Given a string <code>s</code>&nbsp;of lowercase letters, you need to find the maximum number of <strong>non-empty</strong> substrings of&nbsp;<code>s</code>&nbsp;that meet the following conditions:</p>
15+
16+
<ol>
17+
<li>The substrings do not overlap, that is for any two substrings <code>s[i..j]</code> and <code>s[k..l]</code>, either <code>j &lt; k</code> or <code>i &gt; l</code>&nbsp;is true.</li>
18+
<li>A substring that contains a certain character&nbsp;<code>c</code>&nbsp;must also contain all occurrences of <code>c</code>.</li>
19+
</ol>
20+
21+
<p>Find <em>the maximum number of substrings that meet the above conditions</em>. If there are multiple solutions with the same number of substrings, <em>return the one with minimum total length.&nbsp;</em>It can be shown that there exists a unique solution of minimum total length.</p>
22+
23+
<p>Notice that you can return the substrings in <strong>any</strong> order.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> s = &quot;adefaddaccc&quot;
30+
<strong>Output:</strong> [&quot;e&quot;,&quot;f&quot;,&quot;ccc&quot;]
31+
<b>Explanation:</b>&nbsp;The following are all the possible substrings that meet the conditions:
32+
[
33+
&nbsp; &quot;adefaddaccc&quot;
34+
&nbsp; &quot;adefadda&quot;,
35+
&nbsp; &quot;ef&quot;,
36+
&nbsp; &quot;e&quot;,
37+
&quot;f&quot;,
38+
&nbsp; &quot;ccc&quot;,
39+
]
40+
If we choose the first string, we cannot choose anything else and we&#39;d get only 1. If we choose &quot;adefadda&quot;, we are left with &quot;ccc&quot; which is the only one that doesn&#39;t overlap, thus obtaining 2 substrings. Notice also, that it&#39;s not optimal to choose &quot;ef&quot; since it can be split into two. Therefore, the optimal way is to choose [&quot;e&quot;,&quot;f&quot;,&quot;ccc&quot;] which gives us 3 substrings. No other solution of the same number of substrings exist.
41+
</pre>
42+
43+
<p><strong>Example 2:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong> s = &quot;abbaccd&quot;
47+
<strong>Output:</strong> [&quot;d&quot;,&quot;bb&quot;,&quot;cc&quot;]
48+
<b>Explanation: </b>Notice that while the set of substrings [&quot;d&quot;,&quot;abba&quot;,&quot;cc&quot;] also has length 3, it&#39;s considered incorrect since it has larger total length.
49+
</pre>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
56+
<li><code>s</code>&nbsp;contains only lowercase English letters.</li>
57+
</ul>
58+
59+
### Related Topics
60+
[[Greedy](../../tag/greedy/README.md)]
61+
62+
### Hints
63+
<details>
64+
<summary>Hint 1</summary>
65+
Notice that it's impossible for any two valid substrings to overlap unless one is inside another.
66+
</details>
67+
68+
<details>
69+
<summary>Hint 2</summary>
70+
We can start by finding the starting and ending index for each character.
71+
</details>
72+
73+
<details>
74+
<summary>Hint 3</summary>
75+
From these indices, we can form the substrings by expanding each character's range if necessary (if another character exists in the range with smaller/larger starting/ending index).
76+
</details>
77+
78+
<details>
79+
<summary>Hint 4</summary>
80+
Sort the valid substrings by length and greedily take those with the smallest length, discarding the ones that overlap those we took.
81+
</details>

problems/maximum-width-of-binary-tree/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
## [662. Maximum Width of Binary Tree (Medium)](https://leetcode.com/problems/maximum-width-of-binary-tree "二叉树最大宽度")
1313

14-
<p>Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a <b>full binary tree</b>, but some nodes are null.</p>
14+
<p>Given a binary tree, write a function to get the maximum width of the given tree. The maximum width of a tree is the maximum width among all levels.</p>
1515

1616
<p>The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the <code>null</code> nodes between the end-nodes are also counted into the length calculation.</p>
1717

18+
<p>It is <strong>guaranteed</strong> that the answer will in the range of 32-bit signed integer.</p>
19+
1820
<p><b>Example 1:</b></p>
1921

2022
<pre>
@@ -74,11 +76,14 @@
7476
6 7
7577
<b>Output:</b> 8
7678
<b>Explanation:</b>The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
77-
78-
7979
</pre>
8080

81-
<p><b>Note:</b> Answer will in the range of 32-bit signed integer.</p>
81+
<p>&nbsp;</p>
82+
<p><strong>Constraints:</strong></p>
83+
84+
<ul>
85+
<li>The&nbsp;given binary tree will have between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>3000</code>&nbsp;nodes.</li>
86+
</ul>
8287

8388
### Related Topics
8489
[[Tree](../../tag/tree/README.md)]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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](../best-position-for-a-service-centre "Best Position for a Service Centre")
9+
                
10+
[Next >](../find-users-with-valid-e-mails "Find Users With Valid E-Mails")
11+
12+
## [1516. Move Sub-Tree of N-Ary Tree (Hard)](https://leetcode.com/problems/move-sub-tree-of-n-ary-tree "")
13+
14+
15+
16+
### Related Topics
17+
[[Tree](../../tag/tree/README.md)]
18+
19+
### Hints
20+
<details>
21+
<summary>Hint 1</summary>
22+
Disconnect node p from its parent and append it to the children list of node q.
23+
</details>
24+
25+
<details>
26+
<summary>Hint 2</summary>
27+
If q was in the sub-tree of node p (case 1), get the parent node of p and replace p in its children list with q.
28+
</details>
29+
30+
<details>
31+
<summary>Hint 3</summary>
32+
If p was the root of the tree, make q the root of the tree.
33+
</details>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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](../water-bottles "Water Bottles")
9+
                
10+
[Next >](../maximum-number-of-non-overlapping-substrings "Maximum Number of Non-Overlapping Substrings")
11+
12+
## [1519. Number of Nodes in the Sub-Tree With the Same Label (Medium)](https://leetcode.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label "子树中标签相同的节点数")
13+
14+
<p>Given a tree (i.e. a connected, undirected graph that has no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> <code>edges</code>. The <strong>root</strong> of the tree is the node <code>0</code>, and each node of the tree has <strong>a label</strong> which is a lower-case character given in the string <code>labels</code> (i.e. The node with the number <code>i</code> has the label <code>labels[i]</code>).</p>
15+
16+
<p>The <code>edges</code> array is given on the form <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>, which means there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
17+
18+
<p>Return <em>an array of size <code>n</code></em> where <code>ans[i]</code> is the number of nodes in the subtree of the&nbsp;<code>i<sup>th</sup></code>&nbsp;node which have the same label as node <code>i</code>.</p>
19+
20+
<p>A&nbsp;subtree&nbsp;of a tree&nbsp;<code>T</code> is the tree consisting of a node in <code>T</code> and all of its descendant&nbsp;nodes.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e1.jpg" style="width: 441px; height: 321px;" />
25+
<pre>
26+
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = &quot;abaedcd&quot;
27+
<strong>Output:</strong> [2,1,1,1,1,1,1]
28+
<strong>Explanation:</strong> Node 0 has label &#39;a&#39; and its sub-tree has node 2 with label &#39;a&#39; as well, thus the answer is 2. Notice that any node is part of its sub-tree.
29+
Node 1 has a label &#39;b&#39;. The sub-tree of node 1 contains nodes 1,4 and 5, as nodes 4 and 5 have different labels than node 1, the answer is just 1 (the node itself).
30+
</pre>
31+
32+
<p><strong>Example 2:</strong></p>
33+
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e2.jpg" style="width: 381px; height: 321px;" />
34+
<pre>
35+
<strong>Input:</strong> n = 4, edges = [[0,1],[1,2],[0,3]], labels = &quot;bbbb&quot;
36+
<strong>Output:</strong> [4,2,1,1]
37+
<strong>Explanation:</strong> The sub-tree of node 2 contains only node 2, so the answer is 1.
38+
The sub-tree of node 3 contains only node 3, so the answer is 1.
39+
The sub-tree of node 1 contains nodes 1 and 2, both have label &#39;b&#39;, thus the answer is 2.
40+
The sub-tree of node 0 contains nodes 0, 1, 2 and 3, all with label &#39;b&#39;, thus the answer is 4.
41+
</pre>
42+
43+
<p><strong>Example 3:</strong></p>
44+
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/q3e3.jpg" style="width: 381px; height: 321px;" />
45+
<pre>
46+
<strong>Input:</strong> n = 5, edges = [[0,1],[0,2],[1,3],[0,4]], labels = &quot;aabab&quot;
47+
<strong>Output:</strong> [3,2,1,1,1]
48+
</pre>
49+
50+
<p><strong>Example 4:</strong></p>
51+
52+
<pre>
53+
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,3],[3,4],[4,5]], labels = &quot;cbabaa&quot;
54+
<strong>Output:</strong> [1,2,1,1,2,1]
55+
</pre>
56+
57+
<p><strong>Example 5:</strong></p>
58+
59+
<pre>
60+
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6]], labels = &quot;aaabaaa&quot;
61+
<strong>Output:</strong> [6,5,4,1,3,2,1]
62+
</pre>
63+
64+
<p>&nbsp;</p>
65+
<p><strong>Constraints:</strong></p>
66+
67+
<ul>
68+
<li><code>1 &lt;= n &lt;= 10^5</code></li>
69+
<li><code>edges.length == n - 1</code></li>
70+
<li><code>edges[i].length == 2</code></li>
71+
<li><code>0 &lt;= a<sub>i</sub>,&nbsp;b<sub>i</sub> &lt; n</code></li>
72+
<li><code>a<sub>i</sub> !=&nbsp;b<sub>i</sub></code></li>
73+
<li><code>labels.length == n</code></li>
74+
<li><code>labels</code> is consisting of only of lower-case English letters.</li>
75+
</ul>
76+
77+
### Related Topics
78+
[[Depth-first Search](../../tag/depth-first-search/README.md)]
79+
[[Breadth-first Search](../../tag/breadth-first-search/README.md)]
80+
81+
### Hints
82+
<details>
83+
<summary>Hint 1</summary>
84+
Start traversing the tree and each node should return a vector to its parent node.
85+
</details>
86+
87+
<details>
88+
<summary>Hint 2</summary>
89+
The vector should be of length 26 and have the count of all the labels in the sub-tree of this node.
90+
</details>

problems/unique-binary-search-trees/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
2 1 2 3
2929
</pre>
3030

31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= n &lt;= 19</code></li>
36+
</ul>
37+
3138
### Related Topics
3239
[[Tree](../../tag/tree/README.md)]
3340
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]

0 commit comments

Comments
 (0)