Skip to content

Commit 4fe70a9

Browse files
author
Shuo
authored
Merge pull request #720 from openset/develop
Add: desc
2 parents a49e2ac + b6f0b0c commit 4fe70a9

File tree

45 files changed

+1511
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1511
-0
lines changed

problems/3sum-smaller/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@
1111

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

14+
<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>
1415

16+
<p><strong>Example:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> <i>nums</i> = <code>[-2,0,1,3]</code>, and <i>target</i> = 2
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong>&nbsp;Because there are two triplets which sums are less than 2:
22+
&nbsp; [-2,0,1]
23+
[-2,0,3]
24+
</pre>
25+
26+
<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>
1527

1628
### Related Topics
1729
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]

problems/android-unlock-patterns/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,54 @@
1111

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

14+
<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>
1415

16+
<p>&nbsp;</p>
17+
18+
<p><b>Rules for a valid pattern:</b></p>
19+
20+
<ol>
21+
<li>Each pattern must connect at least <b>m</b> keys and at most <b>n</b> keys.</li>
22+
<li>All the keys must be distinct.</li>
23+
<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>
24+
<li>The order of keys used matters.</li>
25+
</ol>
26+
27+
<p>&nbsp;</p>
28+
29+
<pre>
30+
<img src="https://assets.leetcode.com/uploads/2018/10/12/android-unlock.png" style="width: 418px; height: 128px;" /></pre>
31+
32+
<p>&nbsp;</p>
33+
34+
<p><b>Explanation:</b></p>
35+
36+
<pre>
37+
| 1 | 2 | 3 |
38+
| 4 | 5 | 6 |
39+
| 7 | 8 | 9 |</pre>
40+
41+
<p><b>Invalid move:</b> <code>4 - 1 - 3 - 6 </code><br />
42+
Line 1 - 3 passes through key 2 which had not been selected in the pattern.</p>
43+
44+
<p><b>Invalid move:</b> <code>4 - 1 - 9 - 2</code><br />
45+
Line 1 - 9 passes through key 5 which had not been selected in the pattern.</p>
46+
47+
<p><b>Valid move:</b> <code>2 - 4 - 1 - 3 - 6</code><br />
48+
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern</p>
49+
50+
<p><b>Valid move:</b> <code>6 - 5 - 4 - 1 - 9 - 2</code><br />
51+
Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.</p>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>Example:</strong></p>
56+
57+
<div>
58+
<pre>
59+
<strong>Input: </strong>m = <span id="example-input-1-1">1</span>, n = <span id="example-input-1-2">1</span>
60+
<strong>Output: </strong><span id="example-output-1">9</span>
61+
</pre>
1562

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

problems/best-meeting-point/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@
1111

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

14+
<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>
1415

16+
<p><strong>Example:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong>
20+
21+
1 - 0 - 0 - 0 - 1
22+
| | | | |
23+
0 - 0 - 0 - 0 - 0
24+
| | | | |
25+
0 - 0 - 1 - 0 - 0
26+
27+
<strong>Output: 6
28+
29+
Explanation: </strong>Given three people living at <code>(0,0)</code>, <code>(0,4)</code>, and <code>(2,2)</code>:
30+
&nbsp; The point <code>(0,2)</code> is an ideal meeting point, as the total travel distance
31+
&nbsp; of 2+2+2=6 is minimal. So return 6.</pre>
1532

1633
### Related Topics
1734
[[Sort](https://github.com/openset/leetcode/tree/master/tag/sort/README.md)]

problems/binary-tree-longest-consecutive-sequence-ii/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,37 @@
1111

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

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

16+
<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>
17+
18+
<p><b>Example 1:</b></p>
19+
20+
<pre>
21+
<b>Input:</b>
22+
1
23+
/ \
24+
2 3
25+
<b>Output:</b> 2
26+
<b>Explanation:</b> The longest consecutive path is [1, 2] or [2, 1].
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><b>Example 2:</b></p>
32+
33+
<pre>
34+
<b>Input:</b>
35+
2
36+
/ \
37+
1 3
38+
<b>Output:</b> 3
39+
<b>Explanation:</b> The longest consecutive path is [1, 2, 3] or [3, 2, 1].
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><b>Note:</b> All the values of tree nodes are in the range of [-1e7, 1e7].</p>
1545

1646
### Related Topics
1747
[[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)]

problems/binary-tree-longest-consecutive-sequence/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,43 @@
1111

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

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

16+
<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>
17+
18+
<p><strong>Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong>
22+
23+
1
24+
\
25+
3
26+
/ \
27+
2 4
28+
\
29+
5
30+
31+
<strong>Output:</strong> <code>3</code>
32+
33+
<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>
34+
35+
<p><strong>Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:
39+
40+
</strong> 2
41+
\
42+
3
43+
/
44+
2
45+
/
46+
1
47+
48+
<strong>Output: 2
49+
50+
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>
1551

1652
### Related Topics
1753
[[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)]

problems/bold-words-in-string/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@
1111

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

14+
<p>
15+
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.
16+
</p><p>
17+
The returned string should use the least number of tags possible, and of course the tags should form a valid combination.
18+
</p>
19+
<p>
20+
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.
21+
</p>
1422

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

1630
### Related Topics
1731
[[String](https://github.com/openset/leetcode/tree/master/tag/string/README.md)]

problems/boundary-of-binary-tree/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,59 @@
1111

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

14+
<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>
1415

16+
<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>
17+
18+
<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>
19+
20+
<p>The <b>right-most</b> node is also defined by the same way with left and right exchanged.</p>
21+
22+
<p><b>Example 1</b></p>
23+
24+
<pre>
25+
<b>Input:</b>
26+
1
27+
\
28+
2
29+
/ \
30+
3 4
31+
32+
<b>Ouput:</b>
33+
[1, 3, 4, 2]
34+
35+
<b>Explanation:</b>
36+
The root doesn&#39;t have left subtree, so the root itself is left boundary.
37+
The leaves are node 3 and 4.
38+
The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary.
39+
So order them in anti-clockwise without duplicates and we have [1,3,4,2].
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><b>Example 2</b></p>
45+
46+
<pre>
47+
<b>Input:</b>
48+
____1_____
49+
/ \
50+
2 3
51+
/ \ /
52+
4 5 6
53+
/ \ / \
54+
7 8 9 10
55+
56+
<b>Ouput:</b>
57+
[1,2,4,7,8,9,10,6,3]
58+
59+
<b>Explanation:</b>
60+
The left boundary are node 1,2,4. (4 is the left-most node according to definition)
61+
The leaves are node 4,7,8,9,10.
62+
The right boundary are node 1,3,6,10. (10 is the right-most node).
63+
So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].
64+
</pre>
65+
66+
<p>&nbsp;</p>
1567

1668
### Related Topics
1769
[[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)]

problems/candy-crush/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,44 @@
1111

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

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

16+
<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>
17+
18+
<ol>
19+
<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>
20+
<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>
21+
<li>After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.</li>
22+
<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>
23+
</ol>
24+
25+
<p>You need to perform the above rules until the board becomes stable, then return the current board.</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><b>Example:</b></p>
30+
31+
<pre style="white-space: pre-line">
32+
<b>Input:</b>
33+
board =
34+
[[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]]
35+
36+
<b>Output:</b>
37+
[[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]]
38+
39+
<b>Explanation:</b>
40+
<img src="https://assets.leetcode.com/uploads/2018/10/12/candy_crush_example_2.png" style="width: 777px; height: 532px;" />
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><b>Note:</b></p>
46+
47+
<ol>
48+
<li>The length of <code>board</code> will be in the range [3, 50].</li>
49+
<li>The length of <code>board[i]</code> will be in the range [3, 50].</li>
50+
<li>Each <code>board[i][j]</code> will initially start as an integer in the range [1, 2000].</li>
51+
</ol>
1552

1653
### Related Topics
1754
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]

problems/coin-path/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,42 @@
1111

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

14+
<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>
1415

16+
<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>
17+
18+
<p>If there are multiple paths with the same cost, return the lexicographically smallest such path.</p>
19+
20+
<p>If it&#39;s not possible to reach the place indexed N then you need to return an empty array.</p>
21+
22+
<p><b>Example 1:</b></p>
23+
24+
<pre>
25+
<b>Input:</b> [1,2,4,-1,2], 2
26+
<b>Output:</b> [1,3,5]
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><b>Example 2:</b></p>
32+
33+
<pre>
34+
<b>Input:</b> [1,2,4,-1,2], 1
35+
<b>Output:</b> []
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><b>Note:</b></p>
41+
42+
<ol>
43+
<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>
44+
<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>
45+
<li>Length of A is in the range of [1, 1000].</li>
46+
<li>B is in the range of [1, 100].</li>
47+
</ol>
48+
49+
<p>&nbsp;</p>
1550

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

0 commit comments

Comments
 (0)