Skip to content

Add: new #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ LeetCode Problems' Solutions

| # | Title | Solution | Difficulty |
| :-: | - | - | :-: |
| <span id="1392">1392</span> | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix "最长快乐前缀") | [Go](problems/longest-happy-prefix) | Hard |
| <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 |
| <span id="1390">1390</span> | [Four Divisors](https://leetcode.com/problems/four-divisors "四因数") | [Go](problems/four-divisors) | Medium |
| <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 |
| <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 |
| <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 |
| <span id="1386">1386</span> | [Cinema Seat Allocation](https://leetcode.com/problems/cinema-seat-allocation "安排电影院座位") | [Go](problems/cinema-seat-allocation) | Medium |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
Expand Down
94 changes: 94 additions & 0 deletions problems/check-if-there-is-a-valid-path-in-a-grid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../four-divisors "Four Divisors")

[Next >](../longest-happy-prefix "Longest Happy Prefix")

## [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 "检查网格中是否存在有效路径")

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:
<ul>
<li><strong>1</strong> which means a street connecting the left cell and the right cell.</li>
<li><strong>2</strong> which means a street connecting the upper cell and the lower cell.</li>
<li><b>3</b>&nbsp;which means a street connecting the left cell and the lower cell.</li>
<li><b>4</b> which means a street connecting the right cell and the lower cell.</li>
<li><b>5</b> which means a street connecting the left cell and the upper cell.</li>
<li><b>6</b> which means a street connecting the right cell and the upper cell.</li>
</ul>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/main.png" style="width: 450px; height: 708px;" /></p>

<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>

<p><strong>Notice</strong> that you are <strong>not allowed</strong> to change any street.</p>

<p>Return <i>true</i>&nbsp;if there is a valid path in the grid or <em>false</em> otherwise.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/e1.png" style="width: 455px; height: 311px;" />
<pre>
<strong>Input:</strong> grid = [[2,4,3],[6,5,2]]
<strong>Output:</strong> true
<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).
</pre>

<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/e2.png" style="width: 455px; height: 293px;" />
<pre>
<strong>Input:</strong> grid = [[1,2,1],[1,2,1]]
<strong>Output:</strong> false
<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)
</pre>

<p><strong>Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,2]]
<strong>Output:</strong> false
<strong>Explanation:</strong> You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).
</pre>

<p><strong>Example 4:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,1,1,1,1,3]]
<strong>Output:</strong> true
</pre>

<p><strong>Example 5:</strong></p>

<pre>
<strong>Input:</strong> grid = [[2],[2],[2],[2],[2],[2],[6]]
<strong>Output:</strong> true
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 300</code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 6</code></li>
</ul>

### Related Topics
[[Depth-first Search](../../tag/depth-first-search/README.md)]
[[Breadth-first Search](../../tag/breadth-first-search/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Start DFS from the node (0, 0) and follow the path till you stop.
</details>

<details>
<summary>Hint 2</summary>
When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
</details>
77 changes: 77 additions & 0 deletions problems/cinema-seat-allocation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../find-the-distance-value-between-two-arrays "Find the Distance Value Between Two Arrays")

[Next >](../sort-integers-by-the-power-value "Sort Integers by The Power Value")

## [1386. Cinema Seat Allocation (Medium)](https://leetcode.com/problems/cinema-seat-allocation "安排电影院座位")

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/14/cinema_seats_1.png" style="width: 400px; height: 149px;" /></p>

<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>

<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>

<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>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/14/cinema_seats_3.png" style="width: 400px; height: 96px;" /></p>

<pre>
<strong>Input:</strong> n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
<strong>Output:</strong> 4
<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;
</pre>

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

<pre>
<strong>Input:</strong> n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
<strong>Output:</strong> 2
</pre>

<p><strong>Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 10^9</code></li>
<li><code>1 &lt;=&nbsp;reservedSeats.length &lt;= min(10*n, 10^4)</code></li>
<li><code>reservedSeats[i].length == 2</code></li>
<li><code>1&nbsp;&lt;=&nbsp;reservedSeats[i][0] &lt;= n</code></li>
<li><code>1 &lt;=&nbsp;reservedSeats[i][1] &lt;= 10</code></li>
<li>All <code>reservedSeats[i]</code> are distinct.</li>
</ul>

### Related Topics
[[Greedy](../../tag/greedy/README.md)]
[[Array](../../tag/array/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Note you can allocate at most two families in one row.
</details>

<details>
<summary>Hint 2</summary>
Greedily check if you can allocate seats for two families, one family or none.
</details>

<details>
<summary>Hint 3</summary>
Process only rows that appear in the input, for other rows you can always allocate seats for two families.
</details>
79 changes: 79 additions & 0 deletions problems/create-target-array-in-the-given-order/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../pizza-with-3n-slices "Pizza With 3n Slices")

[Next >](../four-divisors "Four Divisors")

## [1389. Create Target Array in the Given Order (Easy)](https://leetcode.com/problems/create-target-array-in-the-given-order "按既定顺序创建目标数组")

<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>

<ul>
<li>Initially <em>target</em> array is empty.</li>
<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>
<li>Repeat the previous step until there are no elements to read in <code>nums</code> and <code>index.</code></li>
</ul>

<p>Return the <em>target</em> array.</p>

<p>It is guaranteed that the insertion operations will be valid.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,2,3,4], index = [0,1,2,2,1]
<strong>Output:</strong> [0,4,1,3,2]
<strong>Explanation:</strong>
nums index target
0 0 [0]
1 1 [0,1]
2 2 [0,1,2]
3 2 [0,1,3,2]
4 1 [0,4,1,3,2]
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4,0], index = [0,1,2,3,0]
<strong>Output:</strong> [0,1,2,3,4]
<strong>Explanation:</strong>
nums index target
1 0 [1]
2 1 [1,2]
3 2 [1,2,3]
4 3 [1,2,3,4]
0 0 [0,1,2,3,4]
</pre>

<p><strong>Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1], index = [0]
<strong>Output:</strong> [1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length, index.length &lt;= 100</code></li>
<li><code>nums.length == index.length</code></li>
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
<li><code>0 &lt;= index[i] &lt;= i</code></li>
</ul>

### Related Topics
[[Array](../../tag/array/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Simulate the process and fill corresponding numbers in the designated spots.
</details>
4 changes: 2 additions & 2 deletions problems/divide-two-integers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
<pre>
<strong>Input:</strong> dividend = 10, divisor = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 10/3 = truncate(3.33333..) = truncate(3) = 3.
<strong>Explanation:</strong> 10/3 = truncate(3.33333..) = 3.
</pre>

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

<pre>
<strong>Input:</strong> dividend = 7, divisor = -3
<strong>Output:</strong> -2
<strong>Explanation:</strong> 7/-3 = truncate(-2.33333..) = truncate(-2) = 3.
<strong>Explanation:</strong> 7/-3 = truncate(-2.33333..) = -2.
</pre>

<p><strong>Note:</strong></p>
Expand Down
72 changes: 72 additions & 0 deletions problems/find-the-distance-value-between-two-arrays/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../total-sales-amount-by-year "Total Sales Amount by Year")

[Next >](../cinema-seat-allocation "Cinema Seat Allocation")

## [1385. Find the Distance Value Between Two Arrays (Easy)](https://leetcode.com/problems/find-the-distance-value-between-two-arrays "两个数组间的距离值")

<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>

<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>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong>
For arr1[0]=4 we have:
|4-10|=6 &gt; d=2
|4-9|=5 &gt; d=2
|4-1|=3 &gt; d=2
|4-8|=4 &gt; d=2
For arr1[1]=5 we have:
|5-10|=5 &gt; d=2
|5-9|=4 &gt; d=2
|5-1|=4 &gt; d=2
|5-8|=3 &gt; d=2
For arr1[2]=8 we have:
<strong>|8-10|=2 &lt;= d=2</strong>
<strong>|8-9|=1 &lt;= d=2</strong>
|8-1|=7 &gt; d=2
<strong>|8-8|=0 &lt;= d=2</strong>
</pre>

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

<pre>
<strong>Input:</strong> arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
<strong>Output:</strong> 2
</pre>

<p><strong>Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= arr1.length, arr2.length &lt;= 500</code></li>
<li><code>-10^3 &lt;= arr1[i], arr2[j] &lt;= 10^3</code></li>
<li><code>0 &lt;= d &lt;= 100</code></li>
</ul>

### Related Topics
[[Array](../../tag/array/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Sort 'arr2' and use binary search to get the closest element for each 'arr1[i]', it gives a time complexity of O(nlogn).
</details>
Loading