Skip to content
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
34 changes: 18 additions & 16 deletions problems/2-keys-keyboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,38 @@

## 650. 2 Keys Keyboard (Medium)

<p>
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:
<p>Initially on a notepad only one character &#39;A&#39; is present. You can perform two operations on this notepad for each step:</p>

<ol>
<li><code>Copy All</code>: You can copy all the characters present on the notepad (partial copy is not allowed).</li>
<li><code>Paste</code>: You can paste the characters which are copied <b>last time</b>.</li>
<li><code>Copy All</code>: You can copy all the characters present on the notepad (partial copy is not allowed).</li>
<li><code>Paste</code>: You can paste the characters which are copied <b>last time</b>.</li>
</ol>
</p>

<p>
Given a number <code>n</code>. You have to get <b>exactly</b> <code>n</code> 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get <code>n</code> 'A'.
</p>
<p>&nbsp;</p>

<p>Given a number <code>n</code>. You have to get <b>exactly</b> <code>n</code> &#39;A&#39; on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get <code>n</code> &#39;A&#39;.</p>

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

<p><b>Example 1:</b><br />
<pre>
<b>Input:</b> 3
<b>Output:</b> 3
<b>Explanation:</b>
Intitally, we have one character 'A'.
Intitally, we have one character &#39;A&#39;.
In step 1, we use <b>Copy All</b> operation.
In step 2, we use <b>Paste</b> operation to get 'AA'.
In step 3, we use <b>Paste</b> operation to get 'AAA'.
In step 2, we use <b>Paste</b> operation to get &#39;AA&#39;.
In step 3, we use <b>Paste</b> operation to get &#39;AAA&#39;.
</pre>
</p>

<p>&nbsp;</p>

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

<p><b>Note:</b><br>
<ol>
<li>The <code>n</code> will be in the range [1, 1000].</li>
<li>The <code>n</code> will be in the range [1, 1000].</li>
</ol>
</p>

<p>&nbsp;</p>

### Related Topics
[[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)]
Expand Down
44 changes: 32 additions & 12 deletions problems/average-salary-departments-vs-company/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

## 615. Average Salary: Departments VS Company (Hard)

Given two tables as below, write a query to display the comparison result (higher/lower/same) of the average salary of employees in a department to the company's average salary.</p>

Given two tables as below, write a query to display the comparison result (higher/lower/same) of the average salary of employees in a department to the company&#39;s average salary.
<p>&nbsp;</p>
Table: <code>salary</code>

<pre>
| id | employee_id | amount | pay_date |
|----|-------------|--------|------------|
Expand All @@ -23,29 +24,48 @@ Table: <code>salary</code>
| 4 | 1 | 7000 | 2017-02-28 |
| 5 | 2 | 6000 | 2017-02-28 |
| 6 | 3 | 8000 | 2017-02-28 |
</pre></p>
</pre>

<p>&nbsp;</p>
The <b>employee_id</b> column refers to the <b>employee_id</b> in the following table <code>employee</code>.

<p>&nbsp;</p>

The <b>employee_id</b> column refers to the <b>employee_id</b> in the following table <code>employee</code>.</p>
<pre>
| employee_id | department_id |
|-------------|---------------|
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
</pre></p>
</pre>

<p>&nbsp;</p>
So for the sample data above, the result is:

<p>&nbsp;</p>

So for the sample data above, the result is:</p>
<pre>
| pay_month | department_id | comparison |
|-----------|---------------|-------------|
| 2017-03 | 1 | higher |
| 2017-03 | 2 | lower |
| 2017-02 | 1 | same |
| 2017-02 | 2 | same |
</pre></p>
</pre>

<p>&nbsp;</p>
<b>Explanation</b>

<p>&nbsp;</p>
In March, the company&#39;s average salary is (9000+6000+10000)/3 = 8333.33...

<p>&nbsp;</p>
The average salary for department &#39;1&#39; is 9000, which is the salary of <b>employee_id</b> &#39;1&#39; since there is only one employee in this department. So the comparison result is &#39;higher&#39; since 9000 &gt; 8333.33 obviously.

<p>&nbsp;</p>
The average salary of department &#39;2&#39; is (6000 + 10000)/2 = 8000, which is the average of <b>employee_id</b> &#39;2&#39; and &#39;3&#39;. So the comparison result is &#39;lower&#39; since 8000 &lt; 8333.33.

<p>&nbsp;</p>
With he same formula for the average salary comparison in February, the result is &#39;same&#39; since both the department &#39;1&#39; and &#39;2&#39; have the same average salary with the company, which is 7000.

<b>Explanation</b></p>
In March, the company's average salary is (9000+6000+10000)/3 = 8333.33...</p>
The average salary for department '1' is 9000, which is the salary of <b>employee_id</b> '1' since there is only one employee in this department. So the comparison result is 'higher' since 9000 > 8333.33 obviously.</p>
The average salary of department '2' is (6000 + 10000)/2 = 8000, which is the average of <b>employee_id</b> '2' and '3'. So the comparison result is 'lower' since 8000 < 8333.33.</p>
With he same formula for the average salary comparison in February, the result is 'same' since both the department '1' and '2' have the same average salary with the company, which is 7000.</p>
<p>&nbsp;</p>
47 changes: 28 additions & 19 deletions problems/beautiful-arrangement/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,46 @@

## 526. Beautiful Arrangement (Medium)

<p>
Suppose you have <b>N</b> integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these <b>N</b> numbers successfully if one of the following is true for the i<sub>th</sub> position (1 <= i <= N) in this array:
<p>Suppose you have <b>N</b> integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these <b>N</b> numbers successfully if one of the following is true for the i<sub>th</sub> position (1 &lt;= i &lt;= N) in this array:</p>

<ol>
<li>The number at the i<sub>th</sub> position is divisible by <b>i</b>.</li>
<li><b>i</b> is divisible by the number at the i<sub>th</sub> position.</li>
<li>The number at the i<sub>th</sub> position is divisible by <b>i</b>.</li>
<li><b>i</b> is divisible by the number at the i<sub>th</sub> position.</li>
</ol>
</p>

<p>
Now given N, how many beautiful arrangements can you construct?
</p>
<p>&nbsp;</p>

<p>Now given N, how many beautiful arrangements can you construct?</p>

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

<p><b>Example 1:</b><br />
<pre>
<b>Input:</b> 2
<b>Output:</b> 2
<b>Explanation:</b>
<br/>The first beautiful arrangement is [1, 2]:
<br/>Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).
<br/>Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).
<br/>The second beautiful arrangement is [2, 1]:
<br/>Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).
<br/>Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.

The first beautiful arrangement is [1, 2]:

Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).

Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).

The second beautiful arrangement is [2, 1]:

Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).

Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.
</pre>
</p>

<p><b>Note:</b><br>
<p>&nbsp;</p>

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

<ol>
<li><b>N</b> is a positive integer and will not exceed 15.</li>
<li><b>N</b> is a positive integer and will not exceed 15.</li>
</ol>
</p>

<p>&nbsp;</p>

### Related Topics
[[Backtracking](https://github.com/openset/leetcode/tree/master/tag/backtracking/README.md)]
Expand Down
20 changes: 11 additions & 9 deletions problems/big-countries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

## 595. Big Countries (Easy)

<p>There is a table <code>World</code> </p>
<p>There is a table <code>World</code></p>

<pre>
+-----------------+------------+------------+--------------+---------------+
| name | continent | area | population | gdp |
Expand All @@ -23,13 +24,13 @@
| Angola | Africa | 1246700 | 20609294 | 100990000 |
+-----------------+------------+------------+--------------+---------------+
</pre>
<p>
A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.
</p><p>
Write a SQL solution to output big countries' name, population and area.
</p>
<p>
For example, according to the above table, we should output:

<p>A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.</p>

<p>Write a SQL solution to output big countries&#39; name, population and area.</p>

<p>For example, according to the above table, we should output:</p>

<pre>
+--------------+-------------+--------------+
| name | population | area |
Expand All @@ -38,4 +39,5 @@ For example, according to the above table, we should output:
| Algeria | 37100000 | 2381741 |
+--------------+-------------+--------------+
</pre>
</p>

<p>&nbsp;</p>
37 changes: 18 additions & 19 deletions problems/bulb-switcher-ii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,50 @@

## 672. Bulb Switcher II (Medium)

<p>
There is a room with <code>n</code> lights which are turned on initially and 4 buttons on the wall. After performing exactly <code>m</code> unknown operations towards buttons, you need to return how many different kinds of status of the <code>n</code> lights could be.
</p>
<p>There is a room with <code>n</code> lights which are turned on initially and 4 buttons on the wall. After performing exactly <code>m</code> unknown operations towards buttons, you need to return how many different kinds of status of the <code>n</code> lights could be.</p>

<p>
Suppose <code>n</code> lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are given below:
<p>Suppose <code>n</code> lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are given below:</p>

<ol>
<li>Flip all the lights.</li>
<li>Flip lights with even numbers.</li>
<li>Flip lights with odd numbers.</li>
<li>Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...</li>
<li>Flip all the lights.</li>
<li>Flip lights with even numbers.</li>
<li>Flip lights with odd numbers.</li>
<li>Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...</li>
</ol>
</p>

<p>&nbsp;</p>

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

<p><b>Example 1:</b><br />
<pre>
<b>Input:</b> n = 1, m = 1.
<b>Output:</b> 2
<b>Explanation:</b> Status can be: [on], [off]
</pre>
</p>

<p>&nbsp;</p>

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

<p><b>Example 2:</b><br />
<pre>
<b>Input:</b> n = 2, m = 1.
<b>Output:</b> 3
<b>Explanation:</b> Status can be: [on, off], [off, on], [off, off]
</pre>
</p>

<p>&nbsp;</p>

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

<p><b>Example 3:</b><br />
<pre>
<b>Input:</b> n = 3, m = 1.
<b>Output:</b> 4
<b>Explanation:</b> Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].
</pre>
</p>

<p><b>Note:</b>
<code>n</code> and <code>m</code> both fit in range [0, 1000].
</p>
<p>&nbsp;</p>

<p><b>Note:</b> <code>n</code> and <code>m</code> both fit in range [0, 1000].</p>

### Related Topics
[[Math](https://github.com/openset/leetcode/tree/master/tag/math/README.md)]
Expand Down
63 changes: 41 additions & 22 deletions problems/cherry-pickup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,35 @@

## 741. Cherry Pickup (Hard)

<p>
In a N x N <code>grid</code> representing a field of cherries, each cell is one of three possible integers.
</p><p>
<li>0 means the cell is empty, so you can pass through;</li>
<li>1 means the cell contains a cherry, that you can pick up and pass through;</li>
<li>-1 means the cell contains a thorn that blocks your way.</li>
</p><p>
Your task is to collect maximum number of cherries possible by following the rules below:
</p><p>
<li>Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with value 0 or 1);</li>
<li>After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells;</li>
<li>When passing through a path cell containing a cherry, you pick it up and the cell becomes an empty cell (0);</li>
<li>If there is no valid path between (0, 0) and (N-1, N-1), then no cherries can be collected.</li>
</p><p>

<p><b>Example 1:</b><br />
<p>In a N x N <code>grid</code> representing a field of cherries, each cell is one of three possible integers.</p>

<p>&nbsp;</p>

<ul>
<li>0 means the cell is empty, so you can pass through;</li>
<li>1 means the cell contains a cherry, that you can pick up and pass through;</li>
<li>-1 means the cell contains a thorn that blocks your way.</li>
</ul>

<p>&nbsp;</p>

<p>Your task is to collect maximum number of cherries possible by following the rules below:</p>

<p>&nbsp;</p>

<ul>
<li>Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with value 0 or 1);</li>
<li>After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells;</li>
<li>When passing through a path cell containing a cherry, you pick it up and the cell becomes an empty cell (0);</li>
<li>If there is no valid path between (0, 0) and (N-1, N-1), then no cherries can be collected.</li>
</ul>

<p>&nbsp;</p>

<p>&nbsp;</p>

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

<pre>
<b>Input:</b> grid =
[[0, 1, -1],
Expand All @@ -39,13 +52,19 @@ The player started at (0, 0) and went down, down, right right to reach (2, 2).
Then, the player went left, up, up, left to return home, picking up one more cherry.
The total number of cherries picked up is 5, and this is the maximum possible.
</pre>
</p>

<p><b>Note:</b>
<li><code>grid</code> is an <code>N</code> by <code>N</code> 2D array, with <code>1 <= N <= 50</code>.</li>
<li>Each <code>grid[i][j]</code> is an integer in the set <code>{-1, 0, 1}</code>.</li>
<li>It is guaranteed that grid[0][0] and grid[N-1][N-1] are not -1.<li>
</p>
<p>&nbsp;</p>

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

<ul>
<li><code>grid</code> is an <code>N</code> by <code>N</code> 2D array, with <code>1 &lt;= N &lt;= 50</code>.</li>
<li>Each <code>grid[i][j]</code> is an integer in the set <code>{-1, 0, 1}</code>.</li>
<li>It is guaranteed that grid[0][0] and grid[N-1][N-1] are not -1.</li>
<li>
<p>&nbsp;</p>
</li>
</ul>

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