Skip to content

Commit cccea33

Browse files
author
Shuo
committed
A: new
1 parent 6c3b55f commit cccea33

File tree

29 files changed

+708
-42
lines changed

29 files changed

+708
-42
lines changed

README.md

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

7171
| # | Title | Solution | Difficulty |
7272
| :-: | - | - | :-: |
73+
| <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 |
74+
| <span id="1514">1514</span> | [Path with Maximum Probability](https://leetcode.com/problems/path-with-maximum-probability "概率最大的路径") | [Go](problems/path-with-maximum-probability) | Medium |
75+
| <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 |
76+
| <span id="1512">1512</span> | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs "好数对的数目") | [Go](problems/number-of-good-pairs) | Easy |
77+
| <span id="1511">1511</span> | [Customer Order Frequency](https://leetcode.com/problems/customer-order-frequency) 🔒 | [MySQL](problems/customer-order-frequency) | Easy |
78+
| <span id="1510">1510</span> | [Stone Game IV](https://leetcode.com/problems/stone-game-iv "石子游戏 IV") | [Go](problems/stone-game-iv) | Hard |
79+
| <span id="1509">1509</span> | [Minimum Difference Between Largest and Smallest Value in Three Moves](https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves "三次操作后最大值与最小值的最小差") | [Go](problems/minimum-difference-between-largest-and-smallest-value-in-three-moves) | Medium |
80+
| <span id="1508">1508</span> | [Range Sum of Sorted Subarray Sums](https://leetcode.com/problems/range-sum-of-sorted-subarray-sums "子数组和排序后的区间和") | [Go](problems/range-sum-of-sorted-subarray-sums) | Medium |
81+
| <span id="1507">1507</span> | [Reformat Date](https://leetcode.com/problems/reformat-date "转变日期格式") | [Go](problems/reformat-date) | Easy |
82+
| <span id="1506">1506</span> | [Find Root of N-Ary Tree](https://leetcode.com/problems/find-root-of-n-ary-tree) 🔒 | [Go](problems/find-root-of-n-ary-tree) | Medium |
7383
| <span id="1505">1505</span> | [Minimum Possible Integer After at Most K Adjacent Swaps On Digits](https://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits "最多 K 次交换相邻数位后得到的最小整数") | [Go](problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits) | Hard |
7484
| <span id="1504">1504</span> | [Count Submatrices With All Ones](https://leetcode.com/problems/count-submatrices-with-all-ones "统计全 1 子矩形") | [Go](problems/count-submatrices-with-all-ones) | Medium |
7585
| <span id="1503">1503</span> | [Last Moment Before All Ants Fall Out of a Plank](https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank "所有蚂蚁掉下来前的最后一刻") | [Go](problems/last-moment-before-all-ants-fall-out-of-a-plank) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../path-with-maximum-probability "Path with Maximum Probability")
9+
                
10+
Next >
11+
12+
## [1515. Best Position for a Service Centre (Hard)](https://leetcode.com/problems/best-position-for-a-service-centre "服务中心的最佳位置")
13+
14+
<p>A delivery company wants to build a new service centre in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the new centre in a position such that <strong>the sum of the euclidean distances to all customers is minimum</strong>.</p>
15+
16+
<p>Given an array <code>positions</code> where <code>positions[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> is the position of the <code>ith</code> customer on the map, return <em>the minimum sum of the euclidean distances</em> to all customers.</p>
17+
18+
<p>In other words, you need to choose the position of the service centre <code>[x<sub>centre</sub>, y<sub>centre</sub>]</code> such that the following formula is minimized:</p>
19+
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_edited.jpg" />
20+
<p>Answers within&nbsp;<code>10^-5</code>&nbsp;of the actual value will be accepted.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e1.jpg" style="width: 377px; height: 362px;" />
25+
<pre>
26+
<strong>Input:</strong> positions = [[0,1],[1,0],[1,2],[2,1]]
27+
<strong>Output:</strong> 4.00000
28+
<strong>Explanation:</strong> As shown, you can see that choosing [x<sub>centre</sub>, y<sub>centre</sub>] = [1, 1] will make the distance to each customer = 1, the sum of all distances is 4 which is the minimum possible we can achieve.
29+
</pre>
30+
31+
<p><strong>Example 2:</strong></p>
32+
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e3.jpg" style="width: 419px; height: 419px;" />
33+
<pre>
34+
<strong>Input:</strong> positions = [[1,1],[3,3]]
35+
<strong>Output:</strong> 2.82843
36+
<strong>Explanation:</strong> The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843
37+
</pre>
38+
39+
<p><strong>Example 3:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> positions = [[1,1]]
43+
<strong>Output:</strong> 0.00000
44+
</pre>
45+
46+
<p><strong>Example 4:</strong></p>
47+
48+
<pre>
49+
<strong>Input:</strong> positions = [[1,1],[0,0],[2,0]]
50+
<strong>Output:</strong> 2.73205
51+
<strong>Explanation:</strong> At the first glance, you may think that locating the centre at [1, 0] will achieve the minimum sum, but locating it at [1, 0] will make the sum of distances = 3.
52+
Try to locate the centre at [1.0, 0.5773502711] you will see that the sum of distances is 2.73205.
53+
Be careful with the precision!
54+
</pre>
55+
56+
<p><strong>Example 5:</strong></p>
57+
58+
<pre>
59+
<strong>Input:</strong> positions = [[0,1],[3,2],[4,5],[7,6],[8,9],[11,1],[2,12]]
60+
<strong>Output:</strong> 32.94036
61+
<strong>Explanation:</strong> You can use [4.3460852395, 4.9813795505] as the position of the centre.
62+
</pre>
63+
64+
<p>&nbsp;</p>
65+
<p><strong>Constraints:</strong></p>
66+
67+
<ul>
68+
<li><code>1 &lt;=&nbsp;positions.length &lt;= 50</code></li>
69+
<li><code>positions[i].length == 2</code></li>
70+
<li><code>0 &lt;=&nbsp;positions[i][0],&nbsp;positions[i][1] &lt;= 100</code></li>
71+
</ul>
72+
73+
### Related Topics
74+
[[Geometry](../../tag/geometry/README.md)]
75+
76+
### Hints
77+
<details>
78+
<summary>Hint 1</summary>
79+
The problem can be reworded as, giving a set of points on a 2d-plane, return the geometric median.
80+
</details>
81+
82+
<details>
83+
<summary>Hint 2</summary>
84+
Loop over each triplet of points (positions[i], positions[j], positions[k]) where i < j < k, get the centre of the circle which goes throw the 3 points, check if all other points lie in this circle.
85+
</details>

problems/combination-sum/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
]
4646
</pre>
4747

48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= candidates.length &lt;= 30</code></li>
53+
<li><code>1 &lt;= candidates[i] &lt;= 200</code></li>
54+
<li>Each element of <code>candidate</code> is unique.</li>
55+
<li><code>1 &lt;= target &lt;= 500</code></li>
56+
</ul>
57+
4858
### Related Topics
4959
[[Array](../../tag/array/README.md)]
5060
[[Backtracking](../../tag/backtracking/README.md)]
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 <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../stone-game-iv "Stone Game IV")
9+
                
10+
[Next >](../number-of-good-pairs "Number of Good Pairs")
11+
12+
## [1511. Customer Order Frequency (Easy)](https://leetcode.com/problems/customer-order-frequency "")
13+
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Create table If Not Exists Customers (customer_id int, name varchar(30), country varchar(30));
2+
Create table If Not Exists Product (product_id int, description varchar(30), price int)
3+
;
4+
Create table If Not Exists Orders (order_id int, customer_id int, product_id int, order_date date, quantity int)
5+
;
6+
Truncate table Customers;
7+
insert into Customers (customer_id, name, country) values ('1', 'Winston', 'USA');
8+
insert into Customers (customer_id, name, country) values ('2', 'Jonathan', 'Peru');
9+
insert into Customers (customer_id, name, country) values ('3', 'Moustafa', 'Egypt');
10+
Truncate table Product;
11+
insert into Product (product_id, description, price) values ('10', 'LC Phone', '300');
12+
insert into Product (product_id, description, price) values ('20', 'LC T-Shirt', '10');
13+
insert into Product (product_id, description, price) values ('30', 'LC Book', '45');
14+
insert into Product (product_id, description, price) values ('40', 'LC Keychain', '2');
15+
Truncate table Orders;
16+
insert into Orders (order_id, customer_id, product_id, order_date, quantity) values ('1', '1', '10', '2020-06-10', '1');
17+
insert into Orders (order_id, customer_id, product_id, order_date, quantity) values ('2', '1', '20', '2020-07-01', '1');
18+
insert into Orders (order_id, customer_id, product_id, order_date, quantity) values ('3', '1', '30', '2020-07-08', '2');
19+
insert into Orders (order_id, customer_id, product_id, order_date, quantity) values ('4', '2', '10', '2020-06-15', '2');
20+
insert into Orders (order_id, customer_id, product_id, order_date, quantity) values ('5', '2', '40', '2020-07-01', '10');
21+
insert into Orders (order_id, customer_id, product_id, order_date, quantity) values ('6', '3', '20', '2020-06-24', '2');
22+
insert into Orders (order_id, customer_id, product_id, order_date, quantity) values ('7', '3', '30', '2020-06-25', '2');
23+
insert into Orders (order_id, customer_id, product_id, order_date, quantity) values ('9', '3', '30', '2020-05-08', '3');

problems/decoded-string-at-index/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ The decoded string is &quot;hahahaha&quot;. The 5th letter is &quot;h&quot;.
5252
<strong>Explanation: </strong>
5353
The decoded string is &quot;a&quot; repeated 8301530446056247680 times. The 1st letter is &quot;a&quot;.
5454
</pre>
55+
</div>
56+
</div>
57+
</div>
5558

5659
<p>&nbsp;</p>
60+
<p><strong>Constraints:</strong></p>
5761

58-
<p><strong>Note:</strong></p>
59-
60-
<ol>
62+
<ul>
6163
<li><code>2 &lt;= S.length &lt;= 100</code></li>
6264
<li><code>S</code>&nbsp;will only contain lowercase letters and digits <code>2</code> through <code>9</code>.</li>
6365
<li><code>S</code>&nbsp;starts with a letter.</li>
6466
<li><code>1 &lt;= K &lt;= 10^9</code></li>
67+
<li>It&#39;s guaranteed that <code>K</code>&nbsp;is less than or equal to the length of the decoded string.</li>
6568
<li>The decoded string is guaranteed to have less than <code>2^63</code> letters.</li>
66-
</ol>
67-
</div>
68-
</div>
69-
</div>
69+
</ul>
7070

7171
### Related Topics
7272
[[Stack](../../tag/stack/README.md)]
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits "Minimum Possible Integer After at Most K Adjacent Swaps On Digits")
9+
                
10+
[Next >](../reformat-date "Reformat Date")
11+
12+
## [1506. Find Root of N-Ary Tree (Medium)](https://leetcode.com/problems/find-root-of-n-ary-tree "")
13+
14+
15+
16+
### Hints
17+
<details>
18+
<summary>Hint 1</summary>
19+
Node with indegree 0 is the root
20+
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../range-sum-of-sorted-subarray-sums "Range Sum of Sorted Subarray Sums")
9+
                
10+
[Next >](../stone-game-iv "Stone Game IV")
11+
12+
## [1509. Minimum Difference Between Largest and Smallest Value in Three Moves (Medium)](https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves "三次操作后最大值与最小值的最小差")
13+
14+
<p>Given an array <code>nums</code>, you are allowed to choose one element of <code>nums</code> and change it by any&nbsp;value in one move.</p>
15+
16+
<p>Return the minimum difference between the largest and smallest value of <code>nums</code>&nbsp;after perfoming at most 3 moves.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums = [5,3,2,4]
23+
<strong>Output:</strong> 0
24+
<strong>Explanation:</strong> Change the array [5,3,2,4] to [<strong>2</strong>,<strong>2</strong>,2,<strong>2</strong>].
25+
The difference between the maximum and minimum is 2-2 = 0.</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [1,5,0,10,14]
31+
<strong>Output:</strong> 1
32+
<strong>Explanation:</strong> Change the array [1,5,0,10,14] to [1,<strong>1</strong>,0,<strong>1</strong>,<strong>1</strong>].
33+
The difference between the maximum and minimum is 1-0 = 1.
34+
</pre>
35+
36+
<p><strong>Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> nums = [6,6,0,1,1,4,6]
40+
<strong>Output:</strong> 2
41+
</pre>
42+
43+
<p><strong>Example 4:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong> nums = [1,5,6,14,15]
47+
<strong>Output:</strong> 1
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= nums.length &lt;= 10^5</code></li>
55+
<li><code>-10^9 &lt;= nums[i] &lt;= 10^9</code></li>
56+
</ul>
57+
58+
### Related Topics
59+
[[Sort](../../tag/sort/README.md)]
60+
[[Array](../../tag/array/README.md)]
61+
62+
### Hints
63+
<details>
64+
<summary>Hint 1</summary>
65+
The minimum difference possible is is obtained by removing 3 elements between the 3 smallest and 3 largest values in the array.
66+
</details>

problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
[< Previous](../count-submatrices-with-all-ones "Count Submatrices With All Ones")
99

10-
Next >
10+
[Next >](../find-root-of-n-ary-tree "Find Root of N-Ary Tree")
1111

1212
## [1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits (Hard)](https://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits "最多 K 次交换相邻数位后得到的最小整数")
1313

problems/most-frequent-subtree-sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ You may assume the sum of values in any subtree is in the range of 32-bit signed
4040
</p>
4141

4242
### Related Topics
43-
[[Hash Table](../../tag/hash-table/README.md)]
4443
[[Tree](../../tag/tree/README.md)]
44+
[[Hash Table](../../tag/hash-table/README.md)]
4545

4646
### Similar Questions
4747
1. [Subtree of Another Tree](../subtree-of-another-tree) (Easy)
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../customer-order-frequency "Customer Order Frequency")
9+
                
10+
[Next >](../number-of-substrings-with-only-1s "Number of Substrings With Only 1s")
11+
12+
## [1512. Number of Good Pairs (Easy)](https://leetcode.com/problems/number-of-good-pairs "好数对的数目")
13+
14+
<p>Given an array of integers&nbsp;<code>nums</code>.</p>
15+
16+
<p>A pair&nbsp;<code>(i,j)</code>&nbsp;is called <em>good</em> if&nbsp;<code>nums[i]</code> == <code>nums[j]</code> and <code>i</code> &lt; <code>j</code>.</p>
17+
18+
<p>Return the number of <em>good</em> pairs.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> nums = [1,2,3,1,1,3]
25+
<strong>Output:</strong> 4
26+
<strong>Explanation: </strong>There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
27+
</pre>
28+
29+
<p><strong>Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [1,1,1,1]
33+
<strong>Output:</strong> 6
34+
<strong>Explanation: </strong>Each pair in the array are <em>good</em>.
35+
</pre>
36+
37+
<p><strong>Example 3:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> nums = [1,2,3]
41+
<strong>Output:</strong> 0
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
49+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
50+
</ul>
51+
52+
### Related Topics
53+
[[Array](../../tag/array/README.md)]
54+
[[Hash Table](../../tag/hash-table/README.md)]
55+
[[Math](../../tag/math/README.md)]
56+
57+
### Hints
58+
<details>
59+
<summary>Hint 1</summary>
60+
Count how many times each number appears. If a number appears n times, then n * (n – 1) // 2 good pairs can be made with this number.
61+
</details>

0 commit comments

Comments
 (0)