Skip to content

Commit 7569133

Browse files
author
Shuo
committed
Add: new
1 parent 1d64e81 commit 7569133

File tree

32 files changed

+855
-26
lines changed

32 files changed

+855
-26
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ LeetCode Problems' Solutions
6262

6363
| # | Title | Solution | Difficulty |
6464
| :-: | - | - | :-: |
65+
| <span id="1363">1363</span> | [Largest Multiple of Three](https://leetcode.com/problems/largest-multiple-of-three "形成三的最大倍数") | [Go](problems/largest-multiple-of-three) | Hard |
66+
| <span id="1362">1362</span> | [Closest Divisors](https://leetcode.com/problems/closest-divisors "最接近的因数") | [Go](problems/closest-divisors) | Medium |
67+
| <span id="1361">1361</span> | [Validate Binary Tree Nodes](https://leetcode.com/problems/validate-binary-tree-nodes "验证二叉树") | [Go](problems/validate-binary-tree-nodes) | Medium |
68+
| <span id="1360">1360</span> | [Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates "日期之间隔几天") | [Go](problems/number-of-days-between-two-dates) | Easy |
69+
| <span id="1359">1359</span> | [Count All Valid Pickup and Delivery Options](https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options "有效的快递序列数目") | [Go](problems/count-all-valid-pickup-and-delivery-options) | Hard |
70+
| <span id="1358">1358</span> | [Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters "包含所有三种字符的子字符串数目") | [Go](problems/number-of-substrings-containing-all-three-characters) | Medium |
71+
| <span id="1357">1357</span> | [Apply Discount Every n Orders](https://leetcode.com/problems/apply-discount-every-n-orders "每隔 n 个顾客打折") | [Go](problems/apply-discount-every-n-orders) | Medium |
72+
| <span id="1356">1356</span> | [Sort Integers by The Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits "根据数字二进制下 1 的数目排序") | [Go](problems/sort-integers-by-the-number-of-1-bits) | Easy |
73+
| <span id="1355">1355</span> | [Activity Participants](https://leetcode.com/problems/activity-participants) 🔒 | [MySQL](problems/activity-participants) | Medium |
6574
| <span id="1354">1354</span> | [Construct Target Array With Multiple Sums](https://leetcode.com/problems/construct-target-array-with-multiple-sums "多次求和构造目标数组") | [Go](problems/construct-target-array-with-multiple-sums) | Hard |
6675
| <span id="1353">1353</span> | [Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended "最多可以参加的会议数目") | [Go](problems/maximum-number-of-events-that-can-be-attended) | Medium |
6776
| <span id="1352">1352</span> | [Product of the Last K Numbers](https://leetcode.com/problems/product-of-the-last-k-numbers "最后 K 个数的乘积") | [Go](problems/product-of-the-last-k-numbers) | Medium |
+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 <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../construct-target-array-with-multiple-sums "Construct Target Array With Multiple Sums")
9+
                
10+
[Next >](../sort-integers-by-the-number-of-1-bits "Sort Integers by The Number of 1 Bits")
11+
12+
## [1355. Activity Participants (Medium)](https://leetcode.com/problems/activity-participants "")
13+
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Create table If Not Exists Friends (id int, name varchar(30), activity varchar(30));
2+
Create table If Not Exists Activities (id int, name varchar(30));
3+
Truncate table Friends;
4+
insert into Friends (id, name, activity) values ('1', 'Jonathan D.', 'Eating');
5+
insert into Friends (id, name, activity) values ('2', 'Jade W.', 'Singing');
6+
insert into Friends (id, name, activity) values ('3', 'Victor J.', 'Singing');
7+
insert into Friends (id, name, activity) values ('4', 'Elvis Q.', 'Eating');
8+
insert into Friends (id, name, activity) values ('5', 'Daniel A.', 'Eating');
9+
insert into Friends (id, name, activity) values ('6', 'Bob B.', 'Horse Riding');
10+
Truncate table Activities;
11+
insert into Activities (id, name) values ('1', 'Eating');
12+
insert into Activities (id, name) values ('2', 'Singing');
13+
insert into Activities (id, name) values ('3', 'Horse Riding');
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 <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../sort-integers-by-the-number-of-1-bits "Sort Integers by The Number of 1 Bits")
9+
                
10+
[Next >](../number-of-substrings-containing-all-three-characters "Number of Substrings Containing All Three Characters")
11+
12+
## [1357. Apply Discount Every n Orders (Medium)](https://leetcode.com/problems/apply-discount-every-n-orders "每隔 n 个顾客打折")
13+
14+
<p>There is&nbsp;a sale in a supermarket, there will be a <code>discount</code> every <code>n</code> customer.<br />
15+
There are some products in the supermarket where the id of the <code>i-th</code> product is <code>products[i]</code> and the price per unit of this product is&nbsp;<code>prices[i]</code>.<br />
16+
The system will count the number of customers and when the <code>n-th</code> customer arrive he/she will have a <code>discount</code> on the bill. (i.e if the cost is <code>x</code> the new cost is <code>x - (discount * x) / 100</code>). Then the system will start counting customers again.<br />
17+
The customer orders a certain amount of each product where <code>product[i]</code> is the id of the <code>i-th</code> product the customer ordered and <code>amount[i]</code> is the number of units the customer ordered of that product.</p>
18+
19+
<p>Implement the <code>Cashier</code> class:</p>
20+
21+
<ul>
22+
<li><code>Cashier(int n, int discount, int[] products, int[] prices)</code> Initializes the object with <code>n</code>, the <code>discount</code>, the <code>products</code>&nbsp;and their <code>prices</code>.</li>
23+
<li><code>double&nbsp;getBill(int[] product, int[] amount)</code>&nbsp;returns the value of the bill and apply the discount if needed. Answers within <code>10^-5</code> of the actual value will be accepted as correct.</li>
24+
</ul>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Example 1:</strong></p>
28+
29+
<pre>
30+
<strong>Input</strong>
31+
[&quot;Cashier&quot;,&quot;getBill&quot;,&quot;getBill&quot;,&quot;getBill&quot;,&quot;getBill&quot;,&quot;getBill&quot;,&quot;getBill&quot;,&quot;getBill&quot;]
32+
[[3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]],[[1,2],[1,2]],[[3,7],[10,10]],[[1,2,3,4,5,6,7],[1,1,1,1,1,1,1]],[[4],[10]],[[7,3],[10,10]],[[7,5,3,1,6,4,2],[10,10,10,9,9,9,7]],[[2,3,5],[5,3,2]]]
33+
<strong>Output</strong>
34+
[null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0]
35+
<strong>Explanation</strong>
36+
Cashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]);
37+
cashier.getBill([1,2],[1,2]); // return 500.0, bill = 1 * 100 + 2 * 200 = 500.
38+
cashier.getBill([3,7],[10,10]); // return 4000.0
39+
cashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]); // return 800.0, The bill was 1600.0 but as this is the third customer, he has a discount of 50% which means his bill is only 1600 - 1600 * (50 / 100) = 800.
40+
cashier.getBill([4],[10]); // return 4000.0
41+
cashier.getBill([7,3],[10,10]); // return 4000.0
42+
cashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); // return 7350.0, Bill was 14700.0 but as the system counted three more customers, he will have a 50% discount and the bill becomes 7350.0
43+
cashier.getBill([2,3,5],[5,3,2]); // return 2500.0
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n &lt;= 10^4</code></li>
51+
<li><code>0 &lt;= discount &lt;= 100</code></li>
52+
<li><code>1 &lt;= products.length &lt;= 200</code></li>
53+
<li><code>1 &lt;= products[i] &lt;= 200</code></li>
54+
<li>There are <strong>not</strong> repeated elements in the array <code>products</code>.</li>
55+
<li><code>prices.length == products.length</code></li>
56+
<li><code>1 &lt;= prices[i] &lt;= 1000</code></li>
57+
<li><code>1 &lt;= product.length &lt;= products.length</code></li>
58+
<li><code>product[i]</code> exists in <code>products</code>.</li>
59+
<li><code>amount.length == product.length</code></li>
60+
<li><code>1 &lt;= amount[i] &lt;= 1000</code></li>
61+
<li>At most <code>1000</code> calls will be made to <code>getBill</code>.</li>
62+
<li>Answers within&nbsp;<code>10^-5</code>&nbsp;of the actual value will be accepted as correct.</li>
63+
</ul>
64+
65+
### Related Topics
66+
[[Design](../../tag/design/README.md)]
67+
68+
### Hints
69+
<details>
70+
<summary>Hint 1</summary>
71+
Keep track of the count of the customers.
72+
</details>
73+
74+
<details>
75+
<summary>Hint 2</summary>
76+
Check if the count of the customers is divisible by n then apply the discount formula.
77+
</details>

problems/cheapest-flights-within-k-stops/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
## [787. Cheapest Flights Within K Stops (Medium)](https://leetcode.com/problems/cheapest-flights-within-k-stops "K 站中转内最便宜的航班")
1313

14-
<p>There are <code>n</code> cities connected by&nbsp;<code>m</code> flights. Each fight starts from city&nbsp;<code>u </code>and arrives at&nbsp;<code>v</code> with a price <code>w</code>.</p>
14+
<p>There are <code>n</code> cities connected by&nbsp;<code>m</code> flights. Each flight starts from city&nbsp;<code>u </code>and arrives at&nbsp;<code>v</code> with a price <code>w</code>.</p>
1515

1616
<p>Now given all the cities and flights, together with starting city <code>src</code> and the destination&nbsp;<code>dst</code>, your task is to find the cheapest price from <code>src</code> to <code>dst</code> with up to <code>k</code> stops. If there is no such route, output <code>-1</code>.</p>
1717

problems/closest-divisors/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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](../validate-binary-tree-nodes "Validate Binary Tree Nodes")
9+
                
10+
[Next >](../largest-multiple-of-three "Largest Multiple of Three")
11+
12+
## [1362. Closest Divisors (Medium)](https://leetcode.com/problems/closest-divisors "最接近的因数")
13+
14+
<p>Given an integer <code>num</code>, find the closest two integers in absolute difference whose product equals&nbsp;<code>num + 1</code>&nbsp;or <code>num + 2</code>.</p>
15+
16+
<p>Return the two integers in any order.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> num = 8
23+
<strong>Output:</strong> [3,3]
24+
<strong>Explanation:</strong> For num + 1 = 9, the closest divisors are 3 &amp; 3, for num + 2 = 10, the closest divisors are 2 &amp; 5, hence 3 &amp; 3 is chosen.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> num = 123
31+
<strong>Output:</strong> [5,25]
32+
</pre>
33+
34+
<p><strong>Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> num = 999
38+
<strong>Output:</strong> [40,25]
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= num &lt;= 10^9</code></li>
46+
</ul>
47+
48+
### Related Topics
49+
[[Math](../../tag/math/README.md)]
50+
51+
### Hints
52+
<details>
53+
<summary>Hint 1</summary>
54+
Find the divisors of n+1 and n+2.
55+
</details>
56+
57+
<details>
58+
<summary>Hint 2</summary>
59+
To find the divisors of a number, you only need to iterate to the square root of that number.
60+
</details>

problems/consecutive-numbers-sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<!--|@home https://github.com/openset/leetcode |-->
66
<!--+----------------------------------------------------------------------+-->
77

8-
[< Previous](../unique-letter-string "Unique Letter String")
8+
[< Previous](../count-unique-characters-of-all-substrings-of-a-given-string "Count Unique Characters of All Substrings of a Given String")
99

1010
[Next >](../positions-of-large-groups "Positions of Large Groups")
1111

problems/construct-quad-tree/README.md

+81-15
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,97 @@
1111

1212
## [427. Construct Quad Tree (Medium)](https://leetcode.com/problems/construct-quad-tree "建立四叉树")
1313

14-
<p>We want to use quad trees to store an <code>N x N</code> boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes <strong>until the values in the region it represents are all the same</strong>.</p>
14+
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent the <code>grid</code> with a Quad-Tree.</p>
1515

16-
<p>Each node has another two boolean attributes : <code>isLeaf</code> and <code>val</code>. <code>isLeaf</code> is true if and only if the node is a leaf node. The <code>val</code> attribute for a leaf node contains the value of the region it represents.</p>
16+
<p>Return <em>the root of the Quad-Tree</em> representing the <code>grid</code>.</p>
1717

18-
<p>Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:</p>
18+
<p>Notice that you can assign the value of a node to <strong>True</strong> or <strong>False</strong> when <code>isLeaf</code> is <strong>False</strong>, and both are <strong>accepted</strong> in the answer.</p>
1919

20-
<p>Given the <code>8 x 8</code> grid below, we want to construct the corresponding quad tree:</p>
20+
<p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p>
2121

22-
<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_grid.png" style="height:27%; max-height:300px; max-width:299px; width:27%" /></p>
22+
<ul>
23+
<li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s.&nbsp;</li>
24+
<li><code>isLeaf</code>: True if the node is leaf node on the tree or False if the node has the four children.</li>
25+
</ul>
2326

24-
<p>It can be divided according to the definition above:</p>
27+
<pre>
28+
class Node {
29+
public boolean val;
30+
&nbsp; &nbsp; public boolean isLeaf;
31+
&nbsp; &nbsp; public Node topLeft;
32+
&nbsp; &nbsp; public Node topRight;
33+
&nbsp; &nbsp; public Node bottomLeft;
34+
&nbsp; &nbsp; public Node bottomRight;
35+
}</pre>
2536

26-
<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_grid_divided.png" style="height:100%; max-height:300px; max-width:1107px; width:100%" /></p>
37+
<p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p>
38+
39+
<ol>
40+
<li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>)&nbsp;set <code>isLeaf</code>&nbsp;True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li>
41+
<li>If the current grid has different values, set <code>isLeaf</code> to False and&nbsp;set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li>
42+
<li>Recurse for each of the children with the proper sub-grid.</li>
43+
</ol>
44+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" />
45+
<p>If you want to know more about the Quad-Tree, you can refer to the&nbsp;<a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p>
46+
47+
<p><strong>Quad-Tree&nbsp;format:</strong></p>
48+
49+
<p>The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p>
50+
51+
<p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p>
52+
53+
<p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list&nbsp;<code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p>
2754

2855
<p>&nbsp;</p>
56+
<p><strong>Example 1:</strong></p>
57+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" />
58+
<pre>
59+
<strong>Input:</strong> grid = [[0,1],[1,0]]
60+
<strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]]
61+
<strong>Explanation:</strong> The explanation of this example is shown below:
62+
Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree.
63+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" />
64+
</pre>
2965

30-
<p>The corresponding quad tree should be as following, where each node is represented as a <code>(isLeaf, val)</code> pair.</p>
66+
<p><strong>Example 2:</strong></p>
3167

32-
<p>For the non-leaf&nbsp;nodes,&nbsp;<code>val</code> can be arbitrary, so it is represented as <code>*</code>.</p>
68+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p>
3369

34-
<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_quad_tree.png" style="height:100%; max-height:300px; max-width:836px; width:100%" /></p>
70+
<pre>
71+
<strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
72+
<strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
73+
<strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids.
74+
The topLeft, bottomLeft and bottomRight each has the same value.
75+
The topRight have different values so we divide it into 4 sub-grids where each has the same value.
76+
Explanation is shown in the photo below:
77+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" />
78+
</pre>
3579

36-
<p><strong>Note:</strong></p>
80+
<p><strong>Example 3:</strong></p>
3781

38-
<ol>
39-
<li><code>N</code> is less than <code>1000</code> and guaranteened to be a power of 2.</li>
40-
<li>If you want to know more about the quad tree, you can refer to its <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</li>
41-
</ol>
82+
<pre>
83+
<strong>Input:</strong> grid = [[1,1],[1,1]]
84+
<strong>Output:</strong> [[1,1]]
85+
</pre>
86+
87+
<p><strong>Example 4:</strong></p>
88+
89+
<pre>
90+
<strong>Input:</strong> grid = [[0]]
91+
<strong>Output:</strong> [[1,0]]
92+
</pre>
93+
94+
<p><strong>Example 5:</strong></p>
95+
96+
<pre>
97+
<strong>Input:</strong> grid = [[1,1,0,0],[1,1,0,0],[0,0,1,1],[0,0,1,1]]
98+
<strong>Output:</strong> [[0,1],[1,1],[1,0],[1,0],[1,1]]
99+
</pre>
100+
101+
<p>&nbsp;</p>
102+
<p><strong>Constraints:</strong></p>
103+
104+
<ul>
105+
<li><code>n == grid.length == grid[i].length</code></li>
106+
<li><code>n == 2^x</code> where <code>0 &lt;= x &lt;= 6</code></li>
107+
</ul>

problems/construct-target-array-with-multiple-sums/README.md

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

88
[< Previous](../maximum-number-of-events-that-can-be-attended "Maximum Number of Events That Can Be Attended")
99

10-
Next >
10+
[Next >](../activity-participants "Activity Participants")
1111

1212
## [1354. Construct Target Array With Multiple Sums (Hard)](https://leetcode.com/problems/construct-target-array-with-multiple-sums "多次求和构造目标数组")
1313

0 commit comments

Comments
 (0)