Skip to content

Commit cb477f3

Browse files
author
Shuo
committed
A: new
1 parent 697f63d commit cb477f3

File tree

26 files changed

+850
-347
lines changed

26 files changed

+850
-347
lines changed

README.md

Lines changed: 20 additions & 306 deletions
Large diffs are not rendered by default.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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](../countries-you-can-safely-invest-in "Countries You Can Safely Invest In")
9+
                
10+
[Next >](../last-moment-before-all-ants-fall-out-of-a-plank "Last Moment Before All Ants Fall Out of a Plank")
11+
12+
## [1502. Can Make Arithmetic Progression From Sequence (Easy)](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence "判断能否形成等差数列")
13+
14+
<p>Given an array of numbers <code>arr</code>.&nbsp;A sequence of numbers is called an arithmetic progression&nbsp;if the difference between any two consecutive elements is the same.</p>
15+
16+
<p>Return <code>true</code>&nbsp;if the array can be rearranged to form an arithmetic progression, otherwise, return <code>false</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> arr = [3,5,1]
23+
<strong>Output:</strong> true
24+
<strong>Explanation: </strong>We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> arr = [1,2,4]
31+
<strong>Output:</strong> false
32+
<strong>Explanation: </strong>There is no way to reorder the elements to obtain an arithmetic progression.
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>2 &lt;= arr.length &lt;= 1000</code></li>
40+
<li><code>-10^6 &lt;= arr[i] &lt;= 10^6</code></li>
41+
</ul>
42+
43+
### Related Topics
44+
[[Sort](../../tag/sort/README.md)]
45+
[[Array](../../tag/array/README.md)]
46+
47+
### Hints
48+
<details>
49+
<summary>Hint 1</summary>
50+
Consider that any valid arithmetic progression will be in sorted order.
51+
</details>
52+
53+
<details>
54+
<summary>Hint 2</summary>
55+
Sort the array, then check if the differences of all consecutive elements are equal.
56+
</details>

problems/count-numbers-with-unique-digits/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
&nbsp; excluding <code>11,22,33,44,55,66,77,88,99</code>
2424
</pre>
2525
</div>
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>0 &lt;= n &lt;= 8</code></li>
31+
</ul>
2632

2733
### Related Topics
2834
[[Math](../../tag/math/README.md)]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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](../last-moment-before-all-ants-fall-out-of-a-plank "Last Moment Before All Ants Fall Out of a Plank")
9+
                
10+
[Next >](../minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits "Minimum Possible Integer After at Most K Adjacent Swaps On Digits")
11+
12+
## [1504. Count Submatrices With All Ones (Medium)](https://leetcode.com/problems/count-submatrices-with-all-ones "统计全 1 子矩形")
13+
14+
<p>Given a&nbsp;<code>rows * columns</code>&nbsp;matrix <code>mat</code> of ones and zeros, return how many&nbsp;<strong>submatrices</strong> have all ones.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong>Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> mat = [[1,0,1],
21+
&nbsp; [1,1,0],
22+
&nbsp; [1,1,0]]
23+
<strong>Output:</strong> 13
24+
<strong>Explanation:
25+
</strong>There are <b>6</b> rectangles of side 1x1.
26+
There are <b>2</b> rectangles of side 1x2.
27+
There are <b>3</b> rectangles of side 2x1.
28+
There is <b>1</b> rectangle of side 2x2.
29+
There is <b>1</b> rectangle of side 3x1.
30+
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = <strong>13.</strong>
31+
</pre>
32+
33+
<p><strong>Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> mat = [[0,1,1,0],
37+
&nbsp; [0,1,1,1],
38+
&nbsp; [1,1,1,0]]
39+
<strong>Output:</strong> 24
40+
<strong>Explanation:</strong>
41+
There are <b>8</b> rectangles of side 1x1.
42+
There are <b>5</b> rectangles of side 1x2.
43+
There are <b>2</b> rectangles of side 1x3.
44+
There are <b>4</b> rectangles of side 2x1.
45+
There are <b>2</b> rectangles of side 2x2.
46+
There are <b>2</b> rectangles of side 3x1.
47+
There is <b>1</b> rectangle of side 3x2.
48+
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24<strong>.</strong>
49+
</pre>
50+
51+
<p><strong>Example 3:</strong></p>
52+
53+
<pre>
54+
<strong>Input:</strong> mat = [[1,1,1,1,1,1]]
55+
<strong>Output:</strong> 21
56+
</pre>
57+
58+
<p><strong>Example 4:</strong></p>
59+
60+
<pre>
61+
<strong>Input:</strong> mat = [[1,0,1],[0,1,0],[1,0,1]]
62+
<strong>Output:</strong> 5
63+
</pre>
64+
65+
<p>&nbsp;</p>
66+
<p><strong>Constraints:</strong></p>
67+
68+
<ul>
69+
<li><code>1 &lt;= rows&nbsp;&lt;= 150</code></li>
70+
<li><code>1 &lt;= columns&nbsp;&lt;= 150</code></li>
71+
<li><code>0 &lt;= mat[i][j] &lt;= 1</code></li>
72+
</ul>
73+
74+
### Related Topics
75+
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
76+
77+
### Hints
78+
<details>
79+
<summary>Hint 1</summary>
80+
For each row i, create an array nums where: if mat[i][j] == 0 then nums[j] = 0 else nums[j] = nums[j-1] +1.
81+
</details>
82+
83+
<details>
84+
<summary>Hint 2</summary>
85+
In the row i, number of rectangles between column j and k(inclusive) and ends in row i, is equal to SUM(min(nums[j, .. idx])) where idx go from j to k. Expected solution is O(n^3).
86+
</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 <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../design-a-file-sharing-system "Design a File Sharing System")
9+
                
10+
[Next >](../can-make-arithmetic-progression-from-sequence "Can Make Arithmetic Progression From Sequence")
11+
12+
## [1501. Countries You Can Safely Invest In (Medium)](https://leetcode.com/problems/countries-you-can-safely-invest-in "")
13+
14+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Create table If Not Exists Person (id int, name varchar(15), phone_number varchar(11));
2+
Create table If Not Exists Country (name varchar(15), country_code varchar(3));
3+
Create table If Not Exists Calls (caller_id int, callee_id int, duration int);
4+
Truncate table Person;
5+
insert into Person (id, name, phone_number) values ('3', 'Jonathan', '051-1234567');
6+
insert into Person (id, name, phone_number) values ('12', 'Elvis', '051-7654321');
7+
insert into Person (id, name, phone_number) values ('1', 'Moncef', '212-1234567');
8+
insert into Person (id, name, phone_number) values ('2', 'Maroua', '212-6523651');
9+
insert into Person (id, name, phone_number) values ('7', 'Meir', '972-1234567');
10+
insert into Person (id, name, phone_number) values ('9', 'Rachel', '972-0011100');
11+
Truncate table Country;
12+
insert into Country (name, country_code) values ('Peru', '051');
13+
insert into Country (name, country_code) values ('Israel', '972');
14+
insert into Country (name, country_code) values ('Morocco', '212');
15+
insert into Country (name, country_code) values ('Germany', '049');
16+
insert into Country (name, country_code) values ('Ethiopia', '251');
17+
Truncate table Calls;
18+
insert into Calls (caller_id, callee_id, duration) values ('1', '9', '33');
19+
insert into Calls (caller_id, callee_id, duration) values ('2', '9', '4');
20+
insert into Calls (caller_id, callee_id, duration) values ('1', '2', '59');
21+
insert into Calls (caller_id, callee_id, duration) values ('3', '12', '102');
22+
insert into Calls (caller_id, callee_id, duration) values ('3', '12', '330');
23+
insert into Calls (caller_id, callee_id, duration) values ('12', '3', '5');
24+
insert into Calls (caller_id, callee_id, duration) values ('7', '9', '13');
25+
insert into Calls (caller_id, callee_id, duration) values ('7', '1', '3');
26+
insert into Calls (caller_id, callee_id, duration) values ('9', '7', '1');
27+
insert into Calls (caller_id, callee_id, duration) values ('1', '7', '7');
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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](../max-value-of-equation "Max Value of Equation")
9+
                
10+
[Next >](../countries-you-can-safely-invest-in "Countries You Can Safely Invest In")
11+
12+
## [1500. Design a File Sharing System (Medium)](https://leetcode.com/problems/design-a-file-sharing-system "")
13+
14+
15+
16+
### Related Topics
17+
[[Array](../../tag/array/README.md)]
18+
19+
### Hints
20+
<details>
21+
<summary>Hint 1</summary>
22+
Try to solve it by keeping for each file chunk, the users who have this chunk.
23+
</details>
24+
25+
<details>
26+
<summary>Hint 2</summary>
27+
Try to solve it by keeping all the users in the system with their owned chunks, and when you request a chunk, check all users for it.
28+
</details>

problems/employee-importance/README.md

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

1212
## [690. Employee Importance (Easy)](https://leetcode.com/problems/employee-importance "员工的重要性")
1313

14-
<p>You are given a data structure of employee information, which includes the employee&#39;s <b>unique id</b>, his <b>importance value</b> and his <b>direct</b> subordinates&#39; id.</p>
14+
<p>You are given a data structure of employee information, which includes the employee&#39;s <b>unique id</b>, their&nbsp;<b>importance value</b> and their&nbsp;<b>direct</b> subordinates&#39; id.</p>
1515

1616
<p>For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is <b>not direct</b>.</p>
1717

18-
<p>Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.</p>
18+
<p>Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all their&nbsp;subordinates.</p>
1919

2020
<p><b>Example 1:</b></p>
2121

@@ -35,8 +35,6 @@ Employee 1 has importance value 5, and he has two direct subordinates: employee
3535
<li>The maximum number of employees won&#39;t exceed 2000.</li>
3636
</ol>
3737

38-
<p>&nbsp;</p>
39-
4038
### Related Topics
4139
[[Depth-first Search](../../tag/depth-first-search/README.md)]
4240
[[Breadth-first Search](../../tag/breadth-first-search/README.md)]
Lines changed: 93 additions & 0 deletions
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 <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../can-make-arithmetic-progression-from-sequence "Can Make Arithmetic Progression From Sequence")
9+
                
10+
[Next >](../count-submatrices-with-all-ones "Count Submatrices With All Ones")
11+
12+
## [1503. Last Moment Before All Ants Fall Out of a Plank (Medium)](https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank "所有蚂蚁掉下来前的最后一刻")
13+
14+
<p>We have a wooden&nbsp;plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the&nbsp;plank, each ant moves with speed <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
15+
16+
<p>When two ants moving in two <strong>different</strong> directions meet at some point, they change their directions and continue moving again. Assume changing directions doesn&#39;t take any additional time.</p>
17+
18+
<p>When an ant reaches <strong>one end</strong> of the plank at a time <code>t</code>, it falls out of the plank imediately.</p>
19+
20+
<p>Given an integer <code>n</code> and two integer arrays <code>left</code> and <code>right</code>, the positions of the ants moving to the left and the right.&nbsp;Return <em>the&nbsp;moment</em> when the last ant(s) fall out of the plank.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants.jpg" style="width: 450px; height: 610px;" />
25+
<pre>
26+
<strong>Input:</strong> n = 4, left = [4,3], right = [0,1]
27+
<strong>Output:</strong> 4
28+
<strong>Explanation:</strong> In the image above:
29+
-The ant at index 0 is named A and going to the right.
30+
-The ant at index 1 is named B and going to the right.
31+
-The ant at index 3 is named C and going to the left.
32+
-The ant at index 4 is named D and going to the left.
33+
Note that the last moment when an ant was on the plank is t = 4 second, after that it falls imediately out of the plank. (i.e. We can say that at t = 4.0000000001, there is no ants on the plank).
34+
</pre>
35+
36+
<p><strong>Example 2:</strong></p>
37+
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants2.jpg" style="width: 639px; height: 101px;" />
38+
<pre>
39+
<strong>Input:</strong> n = 7, left = [], right = [0,1,2,3,4,5,6,7]
40+
<strong>Output:</strong> 7
41+
<strong>Explanation:</strong> All ants are going to the right, the ant at index 0 needs 7 seconds to fall.
42+
</pre>
43+
44+
<p><strong>Example 3:</strong></p>
45+
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants3.jpg" style="width: 639px; height: 100px;" />
46+
<pre>
47+
<strong>Input:</strong> n = 7, left = [0,1,2,3,4,5,6,7], right = []
48+
<strong>Output:</strong> 7
49+
<strong>Explanation:</strong> All ants are going to the left, the ant at index 7 needs 7 seconds to fall.
50+
</pre>
51+
52+
<p><strong>Example 4:</strong></p>
53+
54+
<pre>
55+
<strong>Input:</strong> n = 9, left = [5], right = [4]
56+
<strong>Output:</strong> 5
57+
<strong>Explanation:</strong> At t = 1 second, both ants will be at the same intial position but with different direction.
58+
</pre>
59+
60+
<p><strong>Example 5:</strong></p>
61+
62+
<pre>
63+
<strong>Input:</strong> n = 6, left = [6], right = [0]
64+
<strong>Output:</strong> 6
65+
</pre>
66+
67+
<p>&nbsp;</p>
68+
<p><strong>Constraints:</strong></p>
69+
70+
<ul>
71+
<li><code>1 &lt;= n &lt;= 10^4</code></li>
72+
<li><code>0 &lt;= left.length &lt;= n + 1</code></li>
73+
<li><code>0 &lt;= left[i] &lt;= n</code></li>
74+
<li><code>0 &lt;= right.length &lt;= n + 1</code></li>
75+
<li><code>0 &lt;= right[i] &lt;= n</code></li>
76+
<li><code>1 &lt;= left.length + right.length &lt;= n + 1</code></li>
77+
<li>All values of <code>left</code> and <code>right</code> are unique, and each value can appear <strong>only in one</strong> of the two arrays.</li>
78+
</ul>
79+
80+
### Related Topics
81+
[[Brainteaser](../../tag/brainteaser/README.md)]
82+
[[Array](../../tag/array/README.md)]
83+
84+
### Hints
85+
<details>
86+
<summary>Hint 1</summary>
87+
The ants change their way when they meet is equivalent to continue moving without changing their direction.
88+
</details>
89+
90+
<details>
91+
<summary>Hint 2</summary>
92+
Answer is the max distance for one ant to reach the end of the plank in the facing direction.
93+
</details>

problems/lowest-common-ancestor-of-a-binary-search-tree/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
</pre>
3737

3838
<p>&nbsp;</p>
39-
40-
<p><strong>Note:</strong></p>
39+
<p><strong>Constraints:</strong></p>
4140

4241
<ul>
4342
<li>All of the nodes&#39; values will be unique.</li>

0 commit comments

Comments
 (0)