Skip to content

Commit 0486c5b

Browse files
author
Shuo
authored
Merge pull request #626 from openset/develop
Add: new
2 parents f92dd99 + 0163dad commit 0486c5b

File tree

20 files changed

+423
-17
lines changed

20 files changed

+423
-17
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ LeetCode Problems' Solutions
5454

5555
| # | Title | Solution | Difficulty |
5656
| :-: | - | - | :-: |
57+
| <span id="1163">1163</span> | [Last Substring in Lexicographical Order](https://leetcode.com/problems/last-substring-in-lexicographical-order "按字典序排在最后的子串") | [Go](https://github.com/openset/leetcode/tree/master/problems/last-substring-in-lexicographical-order) | Hard |
58+
| <span id="1162">1162</span> | [As Far from Land as Possible](https://leetcode.com/problems/as-far-from-land-as-possible "地图分析") | [Go](https://github.com/openset/leetcode/tree/master/problems/as-far-from-land-as-possible) | Medium |
59+
| <span id="1161">1161</span> | [Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree "最大层内元素和") | [Go](https://github.com/openset/leetcode/tree/master/problems/maximum-level-sum-of-a-binary-tree) | Medium |
60+
| <span id="1160">1160</span> | [Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters "拼写单词") | [Go](https://github.com/openset/leetcode/tree/master/problems/find-words-that-can-be-formed-by-characters) | Easy |
61+
| <span id="1159">1159</span> | [Market Analysis II](https://leetcode.com/problems/market-analysis-ii) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/market-analysis-ii) | Hard |
62+
| <span id="1158">1158</span> | [Market Analysis I](https://leetcode.com/problems/market-analysis-i) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/market-analysis-i) | Medium |
5763
| <span id="1157">1157</span> | [Online Majority Element In Subarray](https://leetcode.com/problems/online-majority-element-in-subarray "子数组中占绝大多数的元素") | [Go](https://github.com/openset/leetcode/tree/master/problems/online-majority-element-in-subarray) | Hard |
5864
| <span id="1156">1156</span> | [Swap For Longest Repeated Character Substring](https://leetcode.com/problems/swap-for-longest-repeated-character-substring "单字符重复子串的最大长度") | [Go](https://github.com/openset/leetcode/tree/master/problems/swap-for-longest-repeated-character-substring) | Medium |
5965
| <span id="1155">1155</span> | [Number of Dice Rolls With Target Sum](https://leetcode.com/problems/number-of-dice-rolls-with-target-sum "掷骰子的N种方法") | [Go](https://github.com/openset/leetcode/tree/master/problems/number-of-dice-rolls-with-target-sum) | Medium |
@@ -142,7 +148,7 @@ LeetCode Problems' Solutions
142148
| <span id="1072">1072</span> | [Flip Columns For Maximum Number of Equal Rows](https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows "按列翻转得到最大值等行数") | [Go](https://github.com/openset/leetcode/tree/master/problems/flip-columns-for-maximum-number-of-equal-rows) | Medium |
143149
| <span id="1071">1071</span> | [Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings "字符串的最大公因子") | [Go](https://github.com/openset/leetcode/tree/master/problems/greatest-common-divisor-of-strings) | Easy |
144150
| <span id="1070">1070</span> | [Product Sales Analysis III](https://leetcode.com/problems/product-sales-analysis-iii) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/product-sales-analysis-iii) | Medium |
145-
| <span id="1069">1069</span> | [Product Sales Analysis II](https://leetcode.com/problems/product-sales-analysis-ii) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/product-sales-analysis-ii) | Easy |
151+
| <span id="1069">1069</span> | [Product Sales Analysis II](https://leetcode.com/problems/product-sales-analysis-ii "销售分析Ⅱ") 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/product-sales-analysis-ii) | Easy |
146152
| <span id="1068">1068</span> | [Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/product-sales-analysis-i) | Easy |
147153
| <span id="1067">1067</span> | [Digit Count in Range](https://leetcode.com/problems/digit-count-in-range "范围内的数字计数") 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/digit-count-in-range) | Hard |
148154
| <span id="1066">1066</span> | [Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii "校园自行车分配 II") 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/campus-bikes-ii) | Medium |
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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](https://github.com/openset/leetcode/tree/master/problems/maximum-level-sum-of-a-binary-tree "Maximum Level Sum of a Binary Tree")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/last-substring-in-lexicographical-order "Last Substring in Lexicographical Order")
11+
12+
## 1162. As Far from Land as Possible (Medium)
13+
14+
<p>Given an N x N <code>grid</code>&nbsp;containing only values <code>0</code> and <code>1</code>, where&nbsp;<code>0</code> represents water&nbsp;and <code>1</code> represents land, find a water cell such that its distance to the nearest land cell is maximized and return the distance.</p>
15+
16+
<p>The distance used in this problem is the <em>Manhattan distance</em>:&nbsp;the distance between two cells <code>(x0, y0)</code> and <code>(x1, y1)</code> is <code>|x0 - x1| + |y0 - y1|</code>.</p>
17+
18+
<p>If no land or water exists in the grid, return <code>-1</code>.</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>Example 1:</strong></p>
23+
24+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex1.JPG" style="width: 185px; height: 87px;" /></strong></p>
25+
26+
<pre>
27+
<strong>Input: </strong><span id="example-input-1-1">[[1,0,1],[0,0,0],[1,0,1]]</span>
28+
<strong>Output: </strong><span id="example-output-1">2</span>
29+
<strong>Explanation: </strong>
30+
The cell (1, 1) is as far as possible from all the land with distance 2.
31+
</pre>
32+
33+
<p><strong>Example 2:</strong></p>
34+
35+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex2.JPG" style="width: 184px; height: 87px;" /></strong></p>
36+
37+
<pre>
38+
<strong>Input: </strong><span id="example-input-2-1">[[1,0,0],[0,0,0],[0,0,0]]</span>
39+
<strong>Output: </strong><span id="example-output-2">4</span>
40+
<strong>Explanation: </strong>
41+
The cell (2, 2) is as far as possible from all the land with distance 4.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><span><strong>Note:</strong></span></p>
47+
48+
<ol>
49+
<li><span><code>1 &lt;= grid.length == grid[0].length&nbsp;&lt;= 100</code></span></li>
50+
<li><span><code>grid[i][j]</code>&nbsp;is <code>0</code> or <code>1</code></span></li>
51+
</ol>
52+
53+
### Related Topics
54+
[[Breadth-first Search](https://github.com/openset/leetcode/tree/master/tag/breadth-first-search/README.md)]
55+
[[Graph](https://github.com/openset/leetcode/tree/master/tag/graph/README.md)]
56+
57+
### Similar Questions
58+
1. [Shortest Distance from All Buildings](https://github.com/openset/leetcode/tree/master/problems/shortest-distance-from-all-buildings) (Hard)
59+
60+
### Hints
61+
<details>
62+
<summary>Hint 1</summary>
63+
Can you think of this problem in a backwards way ?
64+
</details>
65+
66+
<details>
67+
<summary>Hint 2</summary>
68+
Imagine expanding outward from each land cell. What kind of search does that ?
69+
</details>
70+
71+
<details>
72+
<summary>Hint 3</summary>
73+
Use BFS starting from all land cells in the same time.
74+
</details>
75+
76+
<details>
77+
<summary>Hint 4</summary>
78+
When do you reach the furthest water cell?
79+
</details>

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

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

1212
## 188. Best Time to Buy and Sell Stock IV (Hard)
1313

14-
<p>Say you have an array for which the <i>i</i><sup>th</sup> element is the price of a given stock on day <i>i</i>.</p>
14+
<p>Say you have an array for which the <i>i<span style="font-size: 10.8333px;">-</span></i><span style="font-size: 10.8333px;">th</span>&nbsp;element is the price of a given stock on day <i>i</i>.</p>
1515

1616
<p>Design an algorithm to find the maximum profit. You may complete at most <b>k</b> transactions.</p>
1717

problems/brace-expansion-ii/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
### Related Topics
8181
[[String](https://github.com/openset/leetcode/tree/master/tag/string/README.md)]
8282

83+
### Similar Questions
84+
1. [Brace Expansion](https://github.com/openset/leetcode/tree/master/problems/brace-expansion) (Medium)
85+
8386
### Hints
8487
<details>
8588
<summary>Hint 1</summary>

problems/brace-expansion/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
### Similar Questions
2020
1. [Decode String](https://github.com/openset/leetcode/tree/master/problems/decode-string) (Medium)
2121
1. [Letter Case Permutation](https://github.com/openset/leetcode/tree/master/problems/letter-case-permutation) (Easy)
22+
1. [Brace Expansion II](https://github.com/openset/leetcode/tree/master/problems/brace-expansion-ii) (Hard)
2223

2324
### Hints
2425
<details>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ One possible answer is: [0,-3,9,-10,null,5], which represents the following heig
3030
</pre>
3131

3232
### Related Topics
33-
[[Depth-first Search](https://github.com/openset/leetcode/tree/master/tag/depth-first-search/README.md)]
3433
[[Linked List](https://github.com/openset/leetcode/tree/master/tag/linked-list/README.md)]
34+
[[Depth-first Search](https://github.com/openset/leetcode/tree/master/tag/depth-first-search/README.md)]
3535

3636
### Similar Questions
3737
1. [Convert Sorted Array to Binary Search Tree](https://github.com/openset/leetcode/tree/master/problems/convert-sorted-array-to-binary-search-tree) (Easy)

problems/day-of-the-year/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@
5151
<li><code>date[4] == date[7] == &#39;-&#39;</code>, and all other <code>date[i]</code>&#39;s are digits</li>
5252
<li><code>date</code> represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.</li>
5353
</ul>
54+
55+
### Related Topics
56+
[[Math](https://github.com/openset/leetcode/tree/master/tag/math/README.md)]
57+
58+
### Hints
59+
<details>
60+
<summary>Hint 1</summary>
61+
Have a integer array of how many days there are per month. February gets one extra day if its a leap year. Then, we can manually count the ordinal as day + (number of days in months before this one).
62+
</details>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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](https://github.com/openset/leetcode/tree/master/problems/market-analysis-ii "Market Analysis II")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/maximum-level-sum-of-a-binary-tree "Maximum Level Sum of a Binary Tree")
11+
12+
## 1160. Find Words That Can Be Formed by Characters (Easy)
13+
14+
<p>You are given an array of strings&nbsp;<code>words</code>&nbsp;and a string&nbsp;<code>chars</code>.</p>
15+
16+
<p>A string is <em>good</em>&nbsp;if&nbsp;it can be formed by&nbsp;characters from <code>chars</code>&nbsp;(each character&nbsp;can only be used once).</p>
17+
18+
<p>Return the sum of lengths of all good strings in <code>words</code>.</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>Example 1:</strong></p>
23+
24+
<pre>
25+
<strong>Input: </strong>words = <span id="example-input-1-1">[&quot;cat&quot;,&quot;bt&quot;,&quot;hat&quot;,&quot;tree&quot;]</span>, chars = <span id="example-input-1-2">&quot;atach&quot;</span>
26+
<strong>Output: </strong><span id="example-output-1">6</span>
27+
<strong>Explanation: </strong>
28+
The strings that can be formed are &quot;cat&quot; and &quot;hat&quot; so the answer is 3 + 3 = 6.
29+
</pre>
30+
31+
<p><strong>Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input: </strong>words = <span id="example-input-2-1">[&quot;hello&quot;,&quot;world&quot;,&quot;leetcode&quot;]</span>, chars = <span id="example-input-2-2">&quot;welldonehoneyr&quot;</span>
35+
<strong>Output: </strong><span id="example-output-2">10</span>
36+
<strong>Explanation: </strong>
37+
The strings that can be formed are &quot;hello&quot; and &quot;world&quot; so the answer is 5 + 5 = 10.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><span><strong>Note:</strong></span></p>
43+
44+
<ol>
45+
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
46+
<li><code>1 &lt;= words[i].length, chars.length&nbsp;&lt;= 100</code></li>
47+
<li>All strings contain lowercase English letters only.</li>
48+
</ol>
49+
50+
### Related Topics
51+
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
52+
[[Hash Table](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)]
53+
54+
### Hints
55+
<details>
56+
<summary>Hint 1</summary>
57+
Solve the problem for each string in <code>words</code> independently.
58+
</details>
59+
60+
<details>
61+
<summary>Hint 2</summary>
62+
Now try to think in frequency of letters.
63+
</details>
64+
65+
<details>
66+
<summary>Hint 3</summary>
67+
Count how many times each character occurs in string <code>chars</code>.
68+
</details>
69+
70+
<details>
71+
<summary>Hint 4</summary>
72+
To form a string using characters from <code>chars</code>, the frequency of each character in <code>chars</code> must be greater than or equal the frequency of that character in the string to be formed.
73+
</details>

problems/inorder-successor-in-bst/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,34 @@
1111

1212
## 285. Inorder Successor in BST (Medium)
1313

14+
<p>Given a binary search tree and a node in it, find the in-order successor of that node in the BST.</p>
1415

16+
<p>The successor of a node&nbsp;<code>p</code>&nbsp;is the node with the smallest key greater than&nbsp;<code>p.val</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>Example 1:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/23/285_example_1.PNG" style="width: 122px; height: 117px;" />
22+
<pre><strong>Input: </strong>root = <span id="example-input-1-1">[2,1,3]</span>, p = <span id="example-input-1-2">1</span>
23+
<strong>Output: </strong><span id="example-output-1">2</span>
24+
<strong>Explanation: </strong>1&#39;s in-order successor node is 2. Note that both p and the return value is of TreeNode type.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/23/285_example_2.PNG" style="width: 246px; height: 229px;" />
29+
<pre><strong>Input: </strong>root = <span id="example-input-2-1">[5,3,6,2,4,null,null,1]</span>, p = <span id="example-input-2-2">6</span>
30+
<strong>Output: </strong><span id="example-output-2">null</span>
31+
<strong>Explanation: </strong>There is no in-order successor of the current node, so the answer is <code>null</code>.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
36+
<p><strong>Note:</strong></p>
37+
38+
<ol>
39+
<li>If the given node has no in-order successor in the tree, return <code>null</code>.</li>
40+
<li>It&#39;s guaranteed that the values of the tree are unique.</li>
41+
</ol>
1542

1643
### Related Topics
1744
[[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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](https://github.com/openset/leetcode/tree/master/problems/as-far-from-land-as-possible "As Far from Land as Possible")
9+
                
10+
Next >
11+
12+
## 1163. Last Substring in Lexicographical Order (Hard)
13+
14+
<p>Given a string <code>s</code>, return the last substring of <code>s</code> in lexicographical order.</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong>Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input: </strong><span id="example-input-1-1">&quot;abab&quot;</span>
22+
<strong>Output: </strong><span id="example-output-1">&quot;bab&quot;</span>
23+
<strong>Explanation: </strong>The substrings are [&quot;a&quot;, &quot;ab&quot;, &quot;aba&quot;, &quot;abab&quot;, &quot;b&quot;, &quot;ba&quot;, &quot;bab&quot;]. The lexicographically maximum substring is &quot;bab&quot;.
24+
</pre>
25+
26+
<p><strong>Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input: </strong><span id="example-input-2-1">&quot;leetcode&quot;</span>
30+
<strong>Output: </strong><span id="example-output-2">&quot;tcode&quot;</span>
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong>Note:</strong></p>
36+
37+
<ol>
38+
<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
39+
<li><font face="monospace">s</font> contains only lowercase English letters.</li>
40+
</ol>
41+
42+
### Related Topics
43+
[[String](https://github.com/openset/leetcode/tree/master/tag/string/README.md)]
44+
45+
### Hints
46+
<details>
47+
<summary>Hint 1</summary>
48+
Assume that the answer is a sub-string from index i to j. If you add the character at index j+1 you get a better answer.
49+
</details>
50+
51+
<details>
52+
<summary>Hint 2</summary>
53+
The answer is always a suffix of the given string.
54+
</details>
55+
56+
<details>
57+
<summary>Hint 3</summary>
58+
Since the limits are high, we need an efficient data structure.
59+
</details>
60+
61+
<details>
62+
<summary>Hint 4</summary>
63+
Use suffix array.
64+
</details>

problems/market-analysis-i/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](https://github.com/openset/leetcode/tree/master/problems/online-majority-element-in-subarray "Online Majority Element In Subarray")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/market-analysis-ii "Market Analysis II")
11+
12+
## 1158. Market Analysis I (Medium)
13+
14+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Create table If Not Exists Users (user_id int, join_date date, favorite_brand varchar(10));
2+
create table if not exists Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int);
3+
create table if not exists Items (item_id int, item_brand varchar(10));
4+
Truncate table Users;
5+
insert into Users (user_id, join_date, favorite_brand) values ('1', '2018-01-01', 'Lenovo');
6+
insert into Users (user_id, join_date, favorite_brand) values ('2', '2018-02-09', 'Samsung');
7+
insert into Users (user_id, join_date, favorite_brand) values ('3', '2018-01-19', 'LG');
8+
insert into Users (user_id, join_date, favorite_brand) values ('4', '2018-05-21', 'HP');
9+
Truncate table Orders;
10+
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('1', '2019-08-01', '4', '1', '2');
11+
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('2', '2018-08-02', '2', '1', '3');
12+
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('3', '2019-08-03', '3', '2', '3');
13+
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('4', '2018-08-04', '1', '4', '2');
14+
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('5', '2018-08-04', '1', '3', '4');
15+
insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('6', '2019-08-05', '2', '2', '4');
16+
Truncate table Items;
17+
insert into Items (item_id, item_brand) values ('1', 'Samsung');
18+
insert into Items (item_id, item_brand) values ('2', 'Lenovo');
19+
insert into Items (item_id, item_brand) values ('3', 'LG');
20+
insert into Items (item_id, item_brand) values ('4', 'HP');

0 commit comments

Comments
 (0)