Skip to content

A: new #781

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
Apr 12, 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: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ LeetCode Problems' Solutions

| # | Title | Solution | Difficulty |
| :-: | - | - | :-: |
| <span id="1411">1411</span> | [Number of Ways to Paint N × 3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid "给 N x 3 网格图涂色的方案数") | [Go](problems/number-of-ways-to-paint-n-3-grid) | Hard |
| <span id="1410">1410</span> | [HTML Entity Parser](https://leetcode.com/problems/html-entity-parser "HTML 实体解析器") | [Go](problems/html-entity-parser) | Medium |
| <span id="1409">1409</span> | [Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key "查询带键的排列") | [Go](problems/queries-on-a-permutation-with-key) | Medium |
| <span id="1408">1408</span> | [String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array "数组中的字符串匹配") | [Go](problems/string-matching-in-an-array) | Easy |
| <span id="1407">1407</span> | [Top Travellers](https://leetcode.com/problems/top-travellers) 🔒 | [MySQL](problems/top-travellers) | Easy |
| <span id="1406">1406</span> | [Stone Game III](https://leetcode.com/problems/stone-game-iii "石子游戏 III") | [Go](problems/stone-game-iii) | Hard |
| <span id="1405">1405</span> | [Longest Happy String](https://leetcode.com/problems/longest-happy-string "最长快乐字符串") | [Go](problems/longest-happy-string) | Medium |
| <span id="1404">1404</span> | [Number of Steps to Reduce a Number in Binary Representation to One](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one "将二进制表示减到 1 的步骤数") | [Go](problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one) | Medium |
Expand All @@ -70,7 +75,7 @@ LeetCode Problems' Solutions
| <span id="1401">1401</span> | [Circle and Rectangle Overlapping](https://leetcode.com/problems/circle-and-rectangle-overlapping "圆和矩形是否有重叠") | [Go](problems/circle-and-rectangle-overlapping) | Medium |
| <span id="1400">1400</span> | [Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings "构造 K 个回文字符串") | [Go](problems/construct-k-palindrome-strings) | Medium |
| <span id="1399">1399</span> | [Count Largest Group](https://leetcode.com/problems/count-largest-group "统计最大组的数目") | [Go](problems/count-largest-group) | Easy |
| <span id="1398">1398</span> | [Customers Who Bought Products A and B but Not C](https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c) 🔒 | [MySQL](problems/customers-who-bought-products-a-and-b-but-not-c) | Medium |
| <span id="1398">1398</span> | [Customers Who Bought Products A and B but Not C](https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c "购买了产品A和产品B却没有购买产品C的顾客") 🔒 | [MySQL](problems/customers-who-bought-products-a-and-b-but-not-c) | Medium |
| <span id="1397">1397</span> | [Find All Good Strings](https://leetcode.com/problems/find-all-good-strings "找到所有好字符串") | [Go](problems/find-all-good-strings) | Hard |
| <span id="1396">1396</span> | [Design Underground System](https://leetcode.com/problems/design-underground-system "设计地铁系统") | [Go](problems/design-underground-system) | Medium |
| <span id="1395">1395</span> | [Count Number of Teams](https://leetcode.com/problems/count-number-of-teams "统计作战单位数") | [Go](problems/count-number-of-teams) | Medium |
Expand Down Expand Up @@ -474,7 +479,7 @@ LeetCode Problems' Solutions
| <span id="997">997</span> | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge "找到小镇的法官") | [Go](problems/find-the-town-judge) | Easy |
| <span id="996">996</span> | [Number of Squareful Arrays](https://leetcode.com/problems/number-of-squareful-arrays "正方形数组的数目") | [Go](problems/number-of-squareful-arrays) | Hard |
| <span id="995">995</span> | [Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips "K 连续位的最小翻转次数") | [Go](problems/minimum-number-of-k-consecutive-bit-flips) | Hard |
| <span id="994">994</span> | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges "腐烂的橘子") | [Go](problems/rotting-oranges) | Easy |
| <span id="994">994</span> | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges "腐烂的橘子") | [Go](problems/rotting-oranges) | Medium |
| <span id="993">993</span> | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree "二叉树的堂兄弟节点") | [Go](problems/cousins-in-binary-tree) | Easy |
| <span id="992">992</span> | [Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers "K 个不同整数的子数组") | [Go](problems/subarrays-with-k-different-integers) | Hard |
| <span id="991">991</span> | [Broken Calculator](https://leetcode.com/problems/broken-calculator "坏了的计算器") | [Go](problems/broken-calculator) | Medium |
Expand Down
10 changes: 9 additions & 1 deletion problems/best-time-to-buy-and-sell-stock-ii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## [122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii "买卖股票的最佳时机 II")

<p>Say you have an array for which the <em>i</em><sup>th</sup> element is the price of a given stock on day <em>i</em>.</p>
<p>Say you have an array <code>prices</code> for which the <em>i</em><sup>th</sup> element is the price of a given stock on day <em>i</em>.</p>

<p>Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).</p>

Expand Down Expand Up @@ -43,6 +43,14 @@
<strong>Output:</strong> 0
<strong>Explanation:</strong> In this case, no transaction is done, i.e. max profit = 0.</pre>

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

<ul>
<li><code>1 &lt;= prices.length &lt;= 3 * 10 ^ 4</code></li>
<li><code>0 &lt;= prices[i]&nbsp;&lt;= 10 ^ 4</code></li>
</ul>

### Related Topics
[[Greedy](../../tag/greedy/README.md)]
[[Array](../../tag/array/README.md)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

[Next >](../count-largest-group "Count Largest Group")

## [1398. Customers Who Bought Products A and B but Not C (Medium)](https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c "")
## [1398. Customers Who Bought Products A and B but Not C (Medium)](https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c "购买了产品A和产品B却没有购买产品C的顾客")


3 changes: 3 additions & 0 deletions problems/fizz-buzz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ Return:
]
</pre>
</p>

### Similar Questions
1. [Fizz Buzz Multithreaded](../fizz-buzz-multithreaded) (Medium)
6 changes: 4 additions & 2 deletions problems/happy-number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

## [202. Happy Number (Easy)](https://leetcode.com/problems/happy-number "快乐数")

<p>Write an algorithm to determine if a number is &quot;happy&quot;.</p>
<p>Write an algorithm to determine if a number <code>n</code> is &quot;happy&quot;.</p>

<p>A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.</p>
<p>A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it <strong>loops endlessly in a cycle</strong> which does not include 1. Those numbers for which this process <strong>ends in 1</strong> are happy numbers.</p>

<p>Return True if <code>n</code> is a happy number, and False if not.</p>

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

Expand Down
89 changes: 89 additions & 0 deletions problems/html-entity-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!--|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](../queries-on-a-permutation-with-key "Queries on a Permutation With Key")

[Next >](../number-of-ways-to-paint-n-3-grid "Number of Ways to Paint N × 3 Grid")

## [1410. HTML Entity Parser (Medium)](https://leetcode.com/problems/html-entity-parser "HTML 实体解析器")

<p><strong>HTML entity parser</strong> is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself.</p>

<p>The special characters and their entities for HTML are:</p>

<ul>
<li><strong>Quotation Mark:</strong>&nbsp;the entity is <code>&amp;quot;</code> and&nbsp;symbol character is <code>&quot;</code>.</li>
<li><strong>Single Quote&nbsp;Mark:</strong>&nbsp;the entity is <code>&amp;apos;</code> and&nbsp;symbol character is <code>&#39;</code>.</li>
<li><strong>Ampersand:</strong>&nbsp;the entity is <code>&amp;amp;</code> and symbol character is <code>&amp;</code>.</li>
<li><strong>Greater Than Sign:</strong>&nbsp;the entity is <code>&amp;gt;</code>&nbsp;and symbol character is <code>&gt;</code>.</li>
<li><strong>Less Than Sign:</strong>&nbsp;the entity is <code>&amp;lt;</code>&nbsp;and symbol character is <code>&lt;</code>.</li>
<li><strong>Slash:</strong>&nbsp;the entity is <code>&amp;frasl;</code> and&nbsp;symbol character is <code>/</code>.</li>
</ul>

<p>Given the input <code>text</code> string to the HTML parser, you have to implement the entity parser.</p>

<p>Return <em>the text</em> after replacing the entities by the special characters.</p>

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

<pre>
<strong>Input:</strong> text = &quot;&amp;amp; is an HTML entity but &amp;ambassador; is not.&quot;
<strong>Output:</strong> &quot;&amp; is an HTML entity but &amp;ambassador; is not.&quot;
<strong>Explanation:</strong> The parser will replace the &amp;amp; entity by &amp;
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;and I quote: &amp;quot;...&amp;quot;&quot;
<strong>Output:</strong> &quot;and I quote: \&quot;...\&quot;&quot;
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;Stay home! Practice on Leetcode :)&quot;
<strong>Output:</strong> &quot;Stay home! Practice on Leetcode :)&quot;
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;x &amp;gt; y &amp;amp;&amp;amp; x &amp;lt; y is always false&quot;
<strong>Output:</strong> &quot;x &gt; y &amp;&amp; x &lt; y is always false&quot;
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;leetcode.com&amp;frasl;problemset&amp;frasl;all&quot;
<strong>Output:</strong> &quot;leetcode.com/problemset/all&quot;
</pre>

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

<ul>
<li><code>1 &lt;= text.length &lt;= 10^5</code></li>
<li>The string may contain any possible characters out of all the 256&nbsp;ASCII characters.</li>
</ul>

### Related Topics
[[Stack](../../tag/stack/README.md)]
[[String](../../tag/string/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Search the string for all the occurrences of the character '&'.
</details>

<details>
<summary>Hint 2</summary>
For every '&' check if it matches an HTML entity by checking the ';' character and if entity found replace it in the answer.
</details>
4 changes: 2 additions & 2 deletions problems/last-stone-weight/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

## [1046. Last Stone Weight (Easy)](https://leetcode.com/problems/last-stone-weight "最后一块石头的重量")

<p>We have a collection of rocks, each rock has a positive integer weight.</p>
<p>We have a collection of stones, each stone&nbsp;has a positive integer weight.</p>

<p>Each turn, we choose the two <strong>heaviest</strong>&nbsp;rocks&nbsp;and smash them together.&nbsp; Suppose the stones have weights <code>x</code> and <code>y</code> with <code>x &lt;= y</code>.&nbsp; The result of this smash is:</p>
<p>Each turn, we choose the two <strong>heaviest</strong>&nbsp;stones&nbsp;and smash them together.&nbsp; Suppose the stones have weights <code>x</code> and <code>y</code> with <code>x &lt;= y</code>.&nbsp; The result of this smash is:</p>

<ul>
<li>If <code>x == y</code>, both stones are totally destroyed;</li>
Expand Down
2 changes: 1 addition & 1 deletion problems/merge-k-sorted-lists/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
</pre>

### Related Topics
[[Heap](../../tag/heap/README.md)]
[[Linked List](../../tag/linked-list/README.md)]
[[Divide and Conquer](../../tag/divide-and-conquer/README.md)]
[[Heap](../../tag/heap/README.md)]

### Similar Questions
1. [Merge Two Sorted Lists](../merge-two-sorted-lists) (Easy)
Expand Down
79 changes: 79 additions & 0 deletions problems/number-of-ways-to-paint-n-3-grid/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](../html-entity-parser "HTML Entity Parser")

Next >

## [1411. Number of Ways to Paint N × 3 Grid (Hard)](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid "给 N x 3 网格图涂色的方案数")

<p>You have a <code>grid</code> of size <code>n x 3</code> and you want to paint each cell of the grid with exactly&nbsp;one of the three colours: <strong>Red</strong>, <strong>Yellow</strong> or <strong>Green</strong>&nbsp;while making sure that no two adjacent cells have&nbsp;the same colour (i.e no two cells that share vertical or horizontal sides have the same colour).</p>

<p>You are given <code>n</code> the number of rows of the grid.</p>

<p>Return <em>the number of ways</em> you can paint this <code>grid</code>. As the answer may grow large, the answer <strong>must be</strong> computed modulo&nbsp;<code>10^9 + 7</code>.</p>

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

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 12
<strong>Explanation:</strong> There are 12 possible way to paint the grid as shown:
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/26/e1.png" style="width: 450px; height: 289px;" />
</pre>

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

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 54
</pre>

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

<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 246
</pre>

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

<pre>
<strong>Input:</strong> n = 7
<strong>Output:</strong> 106494
</pre>

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

<pre>
<strong>Input:</strong> n = 5000
<strong>Output:</strong> 30228214
</pre>

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

<ul>
<li><code>n == grid.length</code></li>
<li><code>grid[i].length == 3</code></li>
<li><code>1 &lt;= n &lt;= 5000</code></li>
</ul>

### Related Topics
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
We will use Dynamic programming approach. we will try all possible configuration.
</details>

<details>
<summary>Hint 2</summary>
Let dp[idx][prev1col][prev2col][prev3col] be the number of ways to color the rows of the grid from idx to n-1 keeping in mind that the previous row (idx - 1) has colors prev1col, prev2col and prev3col. Build the dp array to get the answer.
</details>
72 changes: 72 additions & 0 deletions problems/queries-on-a-permutation-with-key/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](../string-matching-in-an-array "String Matching in an Array")

[Next >](../html-entity-parser "HTML Entity Parser")

## [1409. Queries on a Permutation With Key (Medium)](https://leetcode.com/problems/queries-on-a-permutation-with-key "查询带键的排列")

<p>Given the array <code>queries</code> of positive integers between <code>1</code> and <code>m</code>, you have to process all <code>queries[i]</code> (from <code>i=0</code> to <code>i=queries.length-1</code>) according to the following rules:</p>

<ul>
<li>In the beginning, you have the permutation <code>P=[1,2,3,...,m]</code>.</li>
<li>For the current <code>i</code>, find the position of <code>queries[i]</code> in the permutation <code>P</code> (<strong>indexing from 0</strong>) and then move this at the beginning of the permutation <code>P.</code>&nbsp;Notice that the position of <code>queries[i]</code> in <code>P</code> is the result for <code>queries[i]</code>.</li>
</ul>

<p>Return an array containing the result for the given <code>queries</code>.</p>

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

<pre>
<strong>Input:</strong> queries = [3,1,2,1], m = 5
<strong>Output:</strong> [2,1,2,1]
<strong>Explanation:</strong> The queries are processed as follow:
For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is <strong>2</strong>, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5].
For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is <strong>1</strong>, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5].
For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is <strong>2</strong>, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5].
For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is <strong>1</strong>, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5].
Therefore, the array containing the result is [2,1,2,1].
</pre>

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

<pre>
<strong>Input:</strong> queries = [4,1,2,2], m = 4
<strong>Output:</strong> [3,1,2,0]
</pre>

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

<pre>
<strong>Input:</strong> queries = [7,5,5,8,3], m = 8
<strong>Output:</strong> [6,5,0,7,5]
</pre>

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

<ul>
<li><code>1 &lt;= m &lt;= 10^3</code></li>
<li><code>1 &lt;= queries.length &lt;= m</code></li>
<li><code>1 &lt;= queries[i] &lt;= m</code></li>
</ul>

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

### Hints
<details>
<summary>Hint 1</summary>
Create the permutation P=[1,2,...,m], it could be a list for example.
</details>

<details>
<summary>Hint 2</summary>
For each i, find the position of queries[i] with a simple scan over P and then move this to the beginning.
</details>
2 changes: 1 addition & 1 deletion problems/rotting-oranges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

[Next >](../minimum-number-of-k-consecutive-bit-flips "Minimum Number of K Consecutive Bit Flips")

## [994. Rotting Oranges (Easy)](https://leetcode.com/problems/rotting-oranges "腐烂的橘子")
## [994. Rotting Oranges (Medium)](https://leetcode.com/problems/rotting-oranges "腐烂的橘子")

<p>In a given grid, each cell can have one of three&nbsp;values:</p>

Expand Down
2 changes: 1 addition & 1 deletion problems/stone-game-iii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[< Previous](../longest-happy-string "Longest Happy String")

Next >
[Next >](../top-travellers "Top Travellers")

## [1406. Stone Game III (Hard)](https://leetcode.com/problems/stone-game-iii "石子游戏 III")

Expand Down
Loading