Skip to content

Add: desc #720

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
Nov 26, 2019
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
12 changes: 12 additions & 0 deletions problems/3sum-smaller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@

## [259. 3Sum Smaller (Medium)](https://leetcode.com/problems/3sum-smaller "较小的三数之和")

<p>Given an array of <i>n</i> integers <i>nums</i> and a <i>target</i>, find the number of index triplets <code>i, j, k</code> with <code>0 &lt;= i &lt; j &lt; k &lt; n</code> that satisfy the condition <code>nums[i] + nums[j] + nums[k] &lt; target</code>.</p>

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

<pre>
<strong>Input:</strong> <i>nums</i> = <code>[-2,0,1,3]</code>, and <i>target</i> = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong>&nbsp;Because there are two triplets which sums are less than 2:
&nbsp; [-2,0,1]
[-2,0,3]
</pre>

<p><b style="font-family: sans-serif, Arial, Verdana, &quot;Trebuchet MS&quot;;">Follow up:</b> Could you solve it in <i>O</i>(<i>n</i><sup>2</sup>) runtime?</p>

### Related Topics
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
Expand Down
47 changes: 47 additions & 0 deletions problems/android-unlock-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,54 @@

## [351. Android Unlock Patterns (Medium)](https://leetcode.com/problems/android-unlock-patterns "安卓系统手势解锁")

<p>Given an Android <b>3x3</b> key lock screen and two integers <b>m</b> and <b>n</b>, where 1 &le; m &le; n &le; 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of <b>m</b> keys and maximum <b>n</b> keys.</p>

<p>&nbsp;</p>

<p><b>Rules for a valid pattern:</b></p>

<ol>
<li>Each pattern must connect at least <b>m</b> keys and at most <b>n</b> keys.</li>
<li>All the keys must be distinct.</li>
<li>If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.</li>
<li>The order of keys used matters.</li>
</ol>

<p>&nbsp;</p>

<pre>
<img src="https://assets.leetcode.com/uploads/2018/10/12/android-unlock.png" style="width: 418px; height: 128px;" /></pre>

<p>&nbsp;</p>

<p><b>Explanation:</b></p>

<pre>
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |</pre>

<p><b>Invalid move:</b> <code>4 - 1 - 3 - 6 </code><br />
Line 1 - 3 passes through key 2 which had not been selected in the pattern.</p>

<p><b>Invalid move:</b> <code>4 - 1 - 9 - 2</code><br />
Line 1 - 9 passes through key 5 which had not been selected in the pattern.</p>

<p><b>Valid move:</b> <code>2 - 4 - 1 - 3 - 6</code><br />
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern</p>

<p><b>Valid move:</b> <code>6 - 5 - 4 - 1 - 9 - 2</code><br />
Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.</p>

<p>&nbsp;</p>

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

<div>
<pre>
<strong>Input: </strong>m = <span id="example-input-1-1">1</span>, n = <span id="example-input-1-2">1</span>
<strong>Output: </strong><span id="example-output-1">9</span>
</pre>

### Related Topics
[[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)]
Expand Down
17 changes: 17 additions & 0 deletions problems/best-meeting-point/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,24 @@

## [296. Best Meeting Point (Hard)](https://leetcode.com/problems/best-meeting-point "最佳的碰头地点")

<p>A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using <a href="http://en.wikipedia.org/wiki/Taxicab_geometry" target="_blank">Manhattan Distance</a>, where distance(p1, p2) = <code>|p2.x - p1.x| + |p2.y - p1.y|</code>.</p>

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

<pre>
<strong>Input:</strong>

1 - 0 - 0 - 0 - 1
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 1 - 0 - 0

<strong>Output: 6

Explanation: </strong>Given three people living at <code>(0,0)</code>, <code>(0,4)</code>, and <code>(2,2)</code>:
&nbsp; The point <code>(0,2)</code> is an ideal meeting point, as the total travel distance
&nbsp; of 2+2+2=6 is minimal. So return 6.</pre>

### Related Topics
[[Sort](https://github.com/openset/leetcode/tree/master/tag/sort/README.md)]
Expand Down
30 changes: 30 additions & 0 deletions problems/binary-tree-longest-consecutive-sequence-ii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,37 @@

## [549. Binary Tree Longest Consecutive Sequence II (Medium)](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii "二叉树中最长的连续序列")

<p>Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree.</p>

<p>Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not valid. On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order.</p>

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

<pre>
<b>Input:</b>
1
/ \
2 3
<b>Output:</b> 2
<b>Explanation:</b> The longest consecutive path is [1, 2] or [2, 1].
</pre>

<p>&nbsp;</p>

<p><b>Example 2:</b></p>

<pre>
<b>Input:</b>
2
/ \
1 3
<b>Output:</b> 3
<b>Explanation:</b> The longest consecutive path is [1, 2, 3] or [3, 2, 1].
</pre>

<p>&nbsp;</p>

<p><b>Note:</b> All the values of tree nodes are in the range of [-1e7, 1e7].</p>

### Related Topics
[[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)]
Expand Down
36 changes: 36 additions & 0 deletions problems/binary-tree-longest-consecutive-sequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,43 @@

## [298. Binary Tree Longest Consecutive Sequence (Medium)](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence "二叉树最长连续序列")

<p>Given a binary tree, find the length of the longest consecutive sequence path.</p>

<p>The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).</p>

<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:</strong>

1
\
3
/ \
2 4
\
5

<strong>Output:</strong> <code>3</code>

<strong>Explanation: </strong>Longest consecutive sequence path is <code>3-4-5</code><span style="font-family: sans-serif, Arial, Verdana, &quot;Trebuchet MS&quot;;">, so return </span><code>3</code><span style="font-family: sans-serif, Arial, Verdana, &quot;Trebuchet MS&quot;;">.</span></pre>

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

<pre>
<strong>Input:

</strong> 2
\
3
/
2
/
1

<strong>Output: 2

Explanation: </strong>Longest consecutive sequence path is <code>2-3</code><span style="font-family: sans-serif, Arial, Verdana, &quot;Trebuchet MS&quot;;">, not </span><code>3-2-1</code><span style="font-family: sans-serif, Arial, Verdana, &quot;Trebuchet MS&quot;;">, so return </span><code>2</code><span style="font-family: sans-serif, Arial, Verdana, &quot;Trebuchet MS&quot;;">.</span></pre>

### Related Topics
[[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)]
Expand Down
14 changes: 14 additions & 0 deletions problems/bold-words-in-string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,21 @@

## [758. Bold Words in String (Easy)](https://leetcode.com/problems/bold-words-in-string "字符串中的加粗单词")

<p>
Given a set of keywords <code>words</code> and a string <code>S</code>, make all appearances of all keywords in <code>S</code> bold. Any letters between <code>&lt;b&gt;</code> and <code>&lt;/b&gt;</code> tags become bold.
</p><p>
The returned string should use the least number of tags possible, and of course the tags should form a valid combination.
</p>
<p>
For example, given that <code>words = ["ab", "bc"]</code> and <code>S = "aabcd"</code>, we should return <code>"a&lt;b&gt;abc&lt;/b&gt;d"</code>. Note that returning <code>"a&lt;b&gt;a&lt;b&gt;b&lt;/b&gt;c&lt;/b&gt;d"</code> would use more tags, so it is incorrect.
</p>

<p><b>Note:</b><ol>
<li><code>words</code> has length in range <code>[0, 50]</code>.</li>
<li><code>words[i]</code> has length in range <code>[1, 10]</code>.</li>
<li><code>S</code> has length in range <code>[0, 500]</code>.</li>
<li>All characters in <code>words[i]</code> and <code>S</code> are lowercase letters.</li>
</ol></p>

### Related Topics
[[String](https://github.com/openset/leetcode/tree/master/tag/string/README.md)]
Expand Down
52 changes: 52 additions & 0 deletions problems/boundary-of-binary-tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,59 @@

## [545. Boundary of Binary Tree (Medium)](https://leetcode.com/problems/boundary-of-binary-tree "二叉树的边界")

<p>Given a binary tree, return the values of its boundary in <b>anti-clockwise</b> direction starting from root. Boundary includes left boundary, leaves, and right boundary in order without duplicate <strong>nodes</strong>.&nbsp; (The values of the nodes may still be duplicates.)</p>

<p><b>Left boundary</b> is defined as the path from root to the <b>left-most</b> node. <b>Right boundary</b> is defined as the path from root to the <b>right-most</b> node. If the root doesn&#39;t have left subtree or right subtree, then the root itself is left boundary or right boundary. Note this definition only applies to the input binary tree, and not applies to any subtrees.</p>

<p>The <b>left-most</b> node is defined as a <b>leaf</b> node you could reach when you always firstly travel to the left subtree if exists. If not, travel to the right subtree. Repeat until you reach a leaf node.</p>

<p>The <b>right-most</b> node is also defined by the same way with left and right exchanged.</p>

<p><b>Example 1</b></p>

<pre>
<b>Input:</b>
1
\
2
/ \
3 4

<b>Ouput:</b>
[1, 3, 4, 2]

<b>Explanation:</b>
The root doesn&#39;t have left subtree, so the root itself is left boundary.
The leaves are node 3 and 4.
The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary.
So order them in anti-clockwise without duplicates and we have [1,3,4,2].
</pre>

<p>&nbsp;</p>

<p><b>Example 2</b></p>

<pre>
<b>Input:</b>
____1_____
/ \
2 3
/ \ /
4 5 6
/ \ / \
7 8 9 10

<b>Ouput:</b>
[1,2,4,7,8,9,10,6,3]

<b>Explanation:</b>
The left boundary are node 1,2,4. (4 is the left-most node according to definition)
The leaves are node 4,7,8,9,10.
The right boundary are node 1,3,6,10. (10 is the right-most node).
So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].
</pre>

<p>&nbsp;</p>

### Related Topics
[[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)]
Expand Down
37 changes: 37 additions & 0 deletions problems/candy-crush/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,44 @@

## [723. Candy Crush (Medium)](https://leetcode.com/problems/candy-crush "粉碎糖果")

<p>This question is about implementing a basic elimination algorithm for Candy Crush.</p>

<p>Given a 2D integer array <code>board</code> representing the grid of candy, different positive integers <code>board[i][j]</code> represent different types of candies. A value of <code>board[i][j] = 0</code> represents that the cell at position <code>(i, j)</code> is empty. The given board represents the state of the game following the player&#39;s move. Now, you need to restore the board to a <i>stable state</i> by crushing candies according to the following rules:</p>

<ol>
<li>If three or more candies of the same type are adjacent vertically or horizontally, &quot;crush&quot; them all at the same time - these positions become empty.</li>
<li>After crushing all candies simultaneously, if an empty space on the board has candies on top of itself, then these candies will drop until they hit a candy or bottom at the same time. (No new candies will drop outside the top boundary.)</li>
<li>After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.</li>
<li>If there does not exist more candies that can be crushed (ie. the board is <i>stable</i>), then return the current board.</li>
</ol>

<p>You need to perform the above rules until the board becomes stable, then return the current board.</p>

<p>&nbsp;</p>

<p><b>Example:</b></p>

<pre style="white-space: pre-line">
<b>Input:</b>
board =
[[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]]

<b>Output:</b>
[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,114],[210,0,0,0,214],[310,0,0,113,314],[410,0,0,213,414],[610,211,112,313,614],[710,311,412,613,714],[810,411,512,713,1014]]

<b>Explanation:</b>
<img src="https://assets.leetcode.com/uploads/2018/10/12/candy_crush_example_2.png" style="width: 777px; height: 532px;" />
</pre>

<p>&nbsp;</p>

<p><b>Note:</b></p>

<ol>
<li>The length of <code>board</code> will be in the range [3, 50].</li>
<li>The length of <code>board[i]</code> will be in the range [3, 50].</li>
<li>Each <code>board[i][j]</code> will initially start as an integer in the range [1, 2000].</li>
</ol>

### Related Topics
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
Expand Down
35 changes: 35 additions & 0 deletions problems/coin-path/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,42 @@

## [656. Coin Path (Hard)](https://leetcode.com/problems/coin-path "金币路径")

<p>Given an array <code>A</code> (index starts at <code>1</code>) consisting of N integers: A<sub>1</sub>, A<sub>2</sub>, ..., A<sub>N</sub>&nbsp;and an integer <code>B</code>. The integer <code>B</code> denotes that from any place (suppose the index is <code>i</code>) in the array <code>A</code>, you can jump to any one of the place in the array <code>A</code> indexed <code>i+1</code>, <code>i+2</code>, &hellip;, <code>i+B</code> if this place can be jumped to. Also, if you step on the index <code>i</code>, you have to pay A<sub>i</sub>&nbsp;coins. If A<sub>i</sub>&nbsp;is -1, it means you can&rsquo;t jump to the place indexed <code>i</code> in the array.</p>

<p>Now, you start from the place indexed <code>1</code> in the array <code>A</code>, and your aim is to reach the place indexed <code>N</code> using the minimum coins. You need to return the path of indexes (starting from 1 to N) in the array you should take to get to the place indexed <code>N</code> using minimum coins.</p>

<p>If there are multiple paths with the same cost, return the lexicographically smallest such path.</p>

<p>If it&#39;s not possible to reach the place indexed N then you need to return an empty array.</p>

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

<pre>
<b>Input:</b> [1,2,4,-1,2], 2
<b>Output:</b> [1,3,5]
</pre>

<p>&nbsp;</p>

<p><b>Example 2:</b></p>

<pre>
<b>Input:</b> [1,2,4,-1,2], 1
<b>Output:</b> []
</pre>

<p>&nbsp;</p>

<p><b>Note:</b></p>

<ol>
<li>Path Pa<sub>1</sub>, Pa<sub>2</sub>, ..., Pa<sub>n</sub>&nbsp;is lexicographically smaller than Pb<sub>1</sub>, Pb<sub>2</sub>, ..., Pb<sub>m</sub>, if and only if at the first <code>i</code> where Pa<sub>i</sub>&nbsp;and Pb<sub>i</sub>&nbsp;differ, Pa<sub>i</sub>&nbsp;&lt; Pb<sub>i</sub>; when no such&nbsp;<code>i</code>&nbsp;exists, then&nbsp;<code>n</code> &lt; <code>m</code>.</li>
<li>A<sub>1</sub> &gt;= 0. A<sub>2</sub>, ..., A<sub>N</sub> (if exist) will in the range of [-1, 100].</li>
<li>Length of A is in the range of [1, 1000].</li>
<li>B is in the range of [1, 100].</li>
</ol>

<p>&nbsp;</p>

### Related Topics
[[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)]
Expand Down
Loading