Skip to content

Commit f643c91

Browse files
author
Shuo
committed
Add: new
1 parent 70a6508 commit f643c91

File tree

29 files changed

+696
-35
lines changed

29 files changed

+696
-35
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="1392">1392</span> | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix "最长快乐前缀") | [Go](problems/longest-happy-prefix) | Hard |
66+
| <span id="1391">1391</span> | [Check if There is a Valid Path in a Grid](https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid "检查网格中是否存在有效路径") | [Go](problems/check-if-there-is-a-valid-path-in-a-grid) | Medium |
67+
| <span id="1390">1390</span> | [Four Divisors](https://leetcode.com/problems/four-divisors "四因数") | [Go](problems/four-divisors) | Medium |
68+
| <span id="1389">1389</span> | [Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order "按既定顺序创建目标数组") | [Go](problems/create-target-array-in-the-given-order) | Easy |
69+
| <span id="1388">1388</span> | [Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices "3n 块披萨") | [Go](problems/pizza-with-3n-slices) | Hard |
70+
| <span id="1387">1387</span> | [Sort Integers by The Power Value](https://leetcode.com/problems/sort-integers-by-the-power-value "将整数按权重排序") | [Go](problems/sort-integers-by-the-power-value) | Medium |
71+
| <span id="1386">1386</span> | [Cinema Seat Allocation](https://leetcode.com/problems/cinema-seat-allocation "安排电影院座位") | [Go](problems/cinema-seat-allocation) | Medium |
72+
| <span id="1385">1385</span> | [Find the Distance Value Between Two Arrays](https://leetcode.com/problems/find-the-distance-value-between-two-arrays "两个数组间的距离值") | [Go](problems/find-the-distance-value-between-two-arrays) | Easy |
73+
| <span id="1384">1384</span> | [Total Sales Amount by Year](https://leetcode.com/problems/total-sales-amount-by-year "按年度列出销售总额") 🔒 | [MySQL](problems/total-sales-amount-by-year) | Hard |
6574
| <span id="1383">1383</span> | [Maximum Performance of a Team](https://leetcode.com/problems/maximum-performance-of-a-team "最大的团队表现值") | [Go](problems/maximum-performance-of-a-team) | Hard |
6675
| <span id="1382">1382</span> | [Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree "将二叉搜索树变平衡") | [Go](problems/balance-a-binary-search-tree) | Medium |
6776
| <span id="1381">1381</span> | [Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation "设计一个支持增量操作的栈") | [Go](problems/design-a-stack-with-increment-operation) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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](../four-divisors "Four Divisors")
9+
                
10+
[Next >](../longest-happy-prefix "Longest Happy Prefix")
11+
12+
## [1391. Check if There is a Valid Path in a Grid (Medium)](https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid "检查网格中是否存在有效路径")
13+
14+
Given a <em>m</em> x <em>n</em> <code>grid</code>. Each cell of the <code>grid</code> represents a street. The street of&nbsp;<code>grid[i][j]</code> can be:
15+
<ul>
16+
<li><strong>1</strong> which means a street connecting the left cell and the right cell.</li>
17+
<li><strong>2</strong> which means a street connecting the upper cell and the lower cell.</li>
18+
<li><b>3</b>&nbsp;which means a street connecting the left cell and the lower cell.</li>
19+
<li><b>4</b> which means a street connecting the right cell and the lower cell.</li>
20+
<li><b>5</b> which means a street connecting the left cell and the upper cell.</li>
21+
<li><b>6</b> which means a street connecting the right cell and the upper cell.</li>
22+
</ul>
23+
24+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/main.png" style="width: 450px; height: 708px;" /></p>
25+
26+
<p>You will initially start at the street of the&nbsp;upper-left cell <code>(0,0)</code>. A valid path in the grid is a path which starts from the upper left&nbsp;cell <code>(0,0)</code> and ends at the bottom-right&nbsp;cell <code>(m - 1, n - 1)</code>. <strong>The path should only follow the streets</strong>.</p>
27+
28+
<p><strong>Notice</strong> that you are <strong>not allowed</strong> to change any street.</p>
29+
30+
<p>Return <i>true</i>&nbsp;if there is a valid path in the grid or <em>false</em> otherwise.</p>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Example 1:</strong></p>
34+
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/e1.png" style="width: 455px; height: 311px;" />
35+
<pre>
36+
<strong>Input:</strong> grid = [[2,4,3],[6,5,2]]
37+
<strong>Output:</strong> true
38+
<strong>Explanation:</strong> As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1).
39+
</pre>
40+
41+
<p><strong>Example 2:</strong></p>
42+
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/e2.png" style="width: 455px; height: 293px;" />
43+
<pre>
44+
<strong>Input:</strong> grid = [[1,2,1],[1,2,1]]
45+
<strong>Output:</strong> false
46+
<strong>Explanation:</strong> As shown you the street at cell (0, 0) is not connected with any street of any other cell and you will get stuck at cell (0, 0)
47+
</pre>
48+
49+
<p><strong>Example 3:</strong></p>
50+
51+
<pre>
52+
<strong>Input:</strong> grid = [[1,1,2]]
53+
<strong>Output:</strong> false
54+
<strong>Explanation:</strong> You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).
55+
</pre>
56+
57+
<p><strong>Example 4:</strong></p>
58+
59+
<pre>
60+
<strong>Input:</strong> grid = [[1,1,1,1,1,1,3]]
61+
<strong>Output:</strong> true
62+
</pre>
63+
64+
<p><strong>Example 5:</strong></p>
65+
66+
<pre>
67+
<strong>Input:</strong> grid = [[2],[2],[2],[2],[2],[2],[6]]
68+
<strong>Output:</strong> true
69+
</pre>
70+
71+
<p>&nbsp;</p>
72+
<p><strong>Constraints:</strong></p>
73+
74+
<ul>
75+
<li><code>m == grid.length</code></li>
76+
<li><code>n == grid[i].length</code></li>
77+
<li><code>1 &lt;= m, n &lt;= 300</code></li>
78+
<li><code>1 &lt;= grid[i][j] &lt;= 6</code></li>
79+
</ul>
80+
81+
### Related Topics
82+
[[Depth-first Search](../../tag/depth-first-search/README.md)]
83+
[[Breadth-first Search](../../tag/breadth-first-search/README.md)]
84+
85+
### Hints
86+
<details>
87+
<summary>Hint 1</summary>
88+
Start DFS from the node (0, 0) and follow the path till you stop.
89+
</details>
90+
91+
<details>
92+
<summary>Hint 2</summary>
93+
When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
94+
</details>
+77
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](../find-the-distance-value-between-two-arrays "Find the Distance Value Between Two Arrays")
9+
                
10+
[Next >](../sort-integers-by-the-power-value "Sort Integers by The Power Value")
11+
12+
## [1386. Cinema Seat Allocation (Medium)](https://leetcode.com/problems/cinema-seat-allocation "安排电影院座位")
13+
14+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/14/cinema_seats_1.png" style="width: 400px; height: 149px;" /></p>
15+
16+
<p>A cinema&nbsp;has <code>n</code>&nbsp;rows of seats, numbered from 1 to <code>n</code>&nbsp;and there are ten&nbsp;seats in each row, labelled from 1&nbsp;to 10&nbsp;as shown in the figure above.</p>
17+
18+
<p>Given the array <code>reservedSeats</code> containing the numbers of seats already reserved, for example, <code>reservedSeats[i]=[3,8]</code>&nbsp;means the seat located in row <strong>3</strong> and labelled with <b>8</b>&nbsp;is already reserved.&nbsp;</p>
19+
20+
<p><em>Return the maximum number of four-person families you can allocate on the cinema&nbsp;seats.</em> A four-person family occupies fours seats <strong>in one row</strong>, that are <strong>next to each other</strong>. Seats across an aisle (such as [3,3]&nbsp;and [3,4]) are not considered to be next to each other, however, It is permissible for the four-person family to be separated by an aisle, but in that case, <strong>exactly two people</strong> have to sit on each side of the aisle.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
25+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/14/cinema_seats_3.png" style="width: 400px; height: 96px;" /></p>
26+
27+
<pre>
28+
<strong>Input:</strong> n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
29+
<strong>Output:</strong> 4
30+
<strong>Explanation:</strong> The figure above shows the optimal allocation for four families, where seats mark with blue are already reserved and contiguous seats mark with orange are for one family.&nbsp;
31+
</pre>
32+
33+
<p><strong>Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
37+
<strong>Output:</strong> 2
38+
</pre>
39+
40+
<p><strong>Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
44+
<strong>Output:</strong> 4
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= n &lt;= 10^9</code></li>
52+
<li><code>1 &lt;=&nbsp;reservedSeats.length &lt;= min(10*n, 10^4)</code></li>
53+
<li><code>reservedSeats[i].length == 2</code></li>
54+
<li><code>1&nbsp;&lt;=&nbsp;reservedSeats[i][0] &lt;= n</code></li>
55+
<li><code>1 &lt;=&nbsp;reservedSeats[i][1] &lt;= 10</code></li>
56+
<li>All <code>reservedSeats[i]</code> are distinct.</li>
57+
</ul>
58+
59+
### Related Topics
60+
[[Greedy](../../tag/greedy/README.md)]
61+
[[Array](../../tag/array/README.md)]
62+
63+
### Hints
64+
<details>
65+
<summary>Hint 1</summary>
66+
Note you can allocate at most two families in one row.
67+
</details>
68+
69+
<details>
70+
<summary>Hint 2</summary>
71+
Greedily check if you can allocate seats for two families, one family or none.
72+
</details>
73+
74+
<details>
75+
<summary>Hint 3</summary>
76+
Process only rows that appear in the input, for other rows you can always allocate seats for two families.
77+
</details>
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 <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../pizza-with-3n-slices "Pizza With 3n Slices")
9+
                
10+
[Next >](../four-divisors "Four Divisors")
11+
12+
## [1389. Create Target Array in the Given Order (Easy)](https://leetcode.com/problems/create-target-array-in-the-given-order "按既定顺序创建目标数组")
13+
14+
<p>Given two arrays of integers&nbsp;<code>nums</code> and <code>index</code>. Your task is to create <em>target</em> array under the following rules:</p>
15+
16+
<ul>
17+
<li>Initially <em>target</em> array is empty.</li>
18+
<li>From left to right read nums[i] and index[i], insert at index <code>index[i]</code>&nbsp;the value <code>nums[i]</code>&nbsp;in&nbsp;<em>target</em> array.</li>
19+
<li>Repeat the previous step until there are no elements to read in <code>nums</code> and <code>index.</code></li>
20+
</ul>
21+
22+
<p>Return the <em>target</em> array.</p>
23+
24+
<p>It is guaranteed that the insertion operations will be valid.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Example 1:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [0,1,2,3,4], index = [0,1,2,2,1]
31+
<strong>Output:</strong> [0,4,1,3,2]
32+
<strong>Explanation:</strong>
33+
nums index target
34+
0 0 [0]
35+
1 1 [0,1]
36+
2 2 [0,1,2]
37+
3 2 [0,1,3,2]
38+
4 1 [0,4,1,3,2]
39+
</pre>
40+
41+
<p><strong>Example 2:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> nums = [1,2,3,4,0], index = [0,1,2,3,0]
45+
<strong>Output:</strong> [0,1,2,3,4]
46+
<strong>Explanation:</strong>
47+
nums index target
48+
1 0 [1]
49+
2 1 [1,2]
50+
3 2 [1,2,3]
51+
4 3 [1,2,3,4]
52+
0 0 [0,1,2,3,4]
53+
</pre>
54+
55+
<p><strong>Example 3:</strong></p>
56+
57+
<pre>
58+
<strong>Input:</strong> nums = [1], index = [0]
59+
<strong>Output:</strong> [1]
60+
</pre>
61+
62+
<p>&nbsp;</p>
63+
<p><strong>Constraints:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= nums.length, index.length &lt;= 100</code></li>
67+
<li><code>nums.length == index.length</code></li>
68+
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
69+
<li><code>0 &lt;= index[i] &lt;= i</code></li>
70+
</ul>
71+
72+
### Related Topics
73+
[[Array](../../tag/array/README.md)]
74+
75+
### Hints
76+
<details>
77+
<summary>Hint 1</summary>
78+
Simulate the process and fill corresponding numbers in the designated spots.
79+
</details>

problems/divide-two-integers/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
<pre>
2323
<strong>Input:</strong> dividend = 10, divisor = 3
2424
<strong>Output:</strong> 3
25-
<strong>Explanation:</strong> 10/3 = truncate(3.33333..) = truncate(3) = 3.
25+
<strong>Explanation:</strong> 10/3 = truncate(3.33333..) = 3.
2626
</pre>
2727

2828
<p><strong>Example 2:</strong></p>
2929

3030
<pre>
3131
<strong>Input:</strong> dividend = 7, divisor = -3
3232
<strong>Output:</strong> -2
33-
<strong>Explanation:</strong> 7/-3 = truncate(-2.33333..) = truncate(-2) = 3.
33+
<strong>Explanation:</strong> 7/-3 = truncate(-2.33333..) = -2.
3434
</pre>
3535

3636
<p><strong>Note:</strong></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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](../total-sales-amount-by-year "Total Sales Amount by Year")
9+
                
10+
[Next >](../cinema-seat-allocation "Cinema Seat Allocation")
11+
12+
## [1385. Find the Distance Value Between Two Arrays (Easy)](https://leetcode.com/problems/find-the-distance-value-between-two-arrays "两个数组间的距离值")
13+
14+
<p>Given two integer arrays <code>arr1</code> and <code>arr2</code>, and the integer <code>d</code>, <em>return the distance value between the two&nbsp;arrays</em>.</p>
15+
16+
<p>The distance value is defined as the number of elements <code>arr1[i]</code> such that there is not any element <code>arr2[j]</code> where <code>|arr1[i]-arr2[j]| &lt;= d</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
23+
<strong>Output:</strong> 2
24+
<strong>Explanation:</strong>
25+
For arr1[0]=4 we have:
26+
|4-10|=6 &gt; d=2
27+
|4-9|=5 &gt; d=2
28+
|4-1|=3 &gt; d=2
29+
|4-8|=4 &gt; d=2
30+
For arr1[1]=5 we have:
31+
|5-10|=5 &gt; d=2
32+
|5-9|=4 &gt; d=2
33+
|5-1|=4 &gt; d=2
34+
|5-8|=3 &gt; d=2
35+
For arr1[2]=8 we have:
36+
<strong>|8-10|=2 &lt;= d=2</strong>
37+
<strong>|8-9|=1 &lt;= d=2</strong>
38+
|8-1|=7 &gt; d=2
39+
<strong>|8-8|=0 &lt;= d=2</strong>
40+
</pre>
41+
42+
<p><strong>Example 2:</strong></p>
43+
44+
<pre>
45+
<strong>Input:</strong> arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
46+
<strong>Output:</strong> 2
47+
</pre>
48+
49+
<p><strong>Example 3:</strong></p>
50+
51+
<pre>
52+
<strong>Input:</strong> arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
53+
<strong>Output:</strong> 1
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= arr1.length, arr2.length &lt;= 500</code></li>
61+
<li><code>-10^3 &lt;= arr1[i], arr2[j] &lt;= 10^3</code></li>
62+
<li><code>0 &lt;= d &lt;= 100</code></li>
63+
</ul>
64+
65+
### Related Topics
66+
[[Array](../../tag/array/README.md)]
67+
68+
### Hints
69+
<details>
70+
<summary>Hint 1</summary>
71+
Sort 'arr2' and use binary search to get the closest element for each 'arr1[i]', it gives a time complexity of O(nlogn).
72+
</details>

0 commit comments

Comments
 (0)