Skip to content

Commit

Permalink
Merge pull request #692 from openset/develop
Browse files Browse the repository at this point in the history
Add: new
  • Loading branch information
awesee authored Oct 29, 2019
2 parents 5b810d6 + 6724d5b commit 582ff1d
Show file tree
Hide file tree
Showing 32 changed files with 490 additions and 37 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ LeetCode Problems' Solutions

| # | Title | Solution | Difficulty |
| :-: | - | - | :-: |
| <span id="1240">1240</span> | [Tiling a Rectangle with the Fewest Squares](https://leetcode.com/problems/tiling-a-rectangle-with-the-fewest-squares "铺瓷砖") | [Go](https://github.com/openset/leetcode/tree/master/problems/tiling-a-rectangle-with-the-fewest-squares) | Hard |
| <span id="1239">1239</span> | [Maximum Length of a Concatenated String with Unique Characters](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters "串联字符串的最大长度") | [Go](https://github.com/openset/leetcode/tree/master/problems/maximum-length-of-a-concatenated-string-with-unique-characters) | Medium |
| <span id="1238">1238</span> | [Circular Permutation in Binary Representation](https://leetcode.com/problems/circular-permutation-in-binary-representation "循环码排列") | [Go](https://github.com/openset/leetcode/tree/master/problems/circular-permutation-in-binary-representation) | Medium |
| <span id="1237">1237</span> | [Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation "找出给定方程的正整数解") | [Go](https://github.com/openset/leetcode/tree/master/problems/find-positive-integer-solution-for-a-given-equation) | Easy |
| <span id="1236">1236</span> | [Web Crawler](https://leetcode.com/problems/web-crawler) 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/web-crawler) | Medium |
| <span id="1235">1235</span> | [Maximum Profit in Job Scheduling](https://leetcode.com/problems/maximum-profit-in-job-scheduling "规划兼职工作") | [Go](https://github.com/openset/leetcode/tree/master/problems/maximum-profit-in-job-scheduling) | Hard |
| <span id="1234">1234</span> | [Replace the Substring for Balanced String](https://leetcode.com/problems/replace-the-substring-for-balanced-string "替换子串得到平衡字符串") | [Go](https://github.com/openset/leetcode/tree/master/problems/replace-the-substring-for-balanced-string) | Medium |
Expand All @@ -71,9 +75,9 @@ LeetCode Problems' Solutions
| <span id="1230">1230</span> | [Toss Strange Coins](https://leetcode.com/problems/toss-strange-coins "抛掷硬币") 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/toss-strange-coins) | Medium |
| <span id="1229">1229</span> | [Meeting Scheduler](https://leetcode.com/problems/meeting-scheduler "安排会议日程") 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/meeting-scheduler) | Medium |
| <span id="1228">1228</span> | [Missing Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression "等差数列中缺失的数字") 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/missing-number-in-arithmetic-progression) | Easy |
| <span id="1227">1227</span> | [Airplane Seat Assignment Probability](https://leetcode.com/problems/airplane-seat-assignment-probability) | [Go](https://github.com/openset/leetcode/tree/master/problems/airplane-seat-assignment-probability) | Medium |
| <span id="1226">1226</span> | [The Dining Philosophers](https://leetcode.com/problems/the-dining-philosophers) | [Go](https://github.com/openset/leetcode/tree/master/problems/the-dining-philosophers) | Medium |
| <span id="1225">1225</span> | [Report Contiguous Dates](https://leetcode.com/problems/report-contiguous-dates) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/report-contiguous-dates) | Hard |
| <span id="1227">1227</span> | [Airplane Seat Assignment Probability](https://leetcode.com/problems/airplane-seat-assignment-probability "飞机座位分配概率") | [Go](https://github.com/openset/leetcode/tree/master/problems/airplane-seat-assignment-probability) | Medium |
| <span id="1226">1226</span> | [The Dining Philosophers](https://leetcode.com/problems/the-dining-philosophers "哲学家进餐") | [Go](https://github.com/openset/leetcode/tree/master/problems/the-dining-philosophers) | Medium |
| <span id="1225">1225</span> | [Report Contiguous Dates](https://leetcode.com/problems/report-contiguous-dates "报告系统状态的连续日期") 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/report-contiguous-dates) | Hard |
| <span id="1224">1224</span> | [Maximum Equal Frequency](https://leetcode.com/problems/maximum-equal-frequency "最大相等频率") | [Go](https://github.com/openset/leetcode/tree/master/problems/maximum-equal-frequency) | Hard |
| <span id="1223">1223</span> | [Dice Roll Simulation](https://leetcode.com/problems/dice-roll-simulation "掷骰子模拟") | [Go](https://github.com/openset/leetcode/tree/master/problems/dice-roll-simulation) | Medium |
| <span id="1222">1222</span> | [Queens That Can Attack the King](https://leetcode.com/problems/queens-that-can-attack-the-king "可以攻击国王的皇后") | [Go](https://github.com/openset/leetcode/tree/master/problems/queens-that-can-attack-the-king) | Medium |
Expand Down
4 changes: 2 additions & 2 deletions problems/airplane-seat-assignment-probability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

[Next >](https://github.com/openset/leetcode/tree/master/problems/missing-number-in-arithmetic-progression "Missing Number In Arithmetic Progression")

## [1227. Airplane Seat Assignment Probability (Medium)](https://leetcode.com/problems/airplane-seat-assignment-probability "")
## [1227. Airplane Seat Assignment Probability (Medium)](https://leetcode.com/problems/airplane-seat-assignment-probability "飞机座位分配概率")

<p><code data-stringify-type="code">n</code>&nbsp;passengers board an airplane with exactly&nbsp;<code data-stringify-type="code">n</code>&nbsp;seats. The first passenger has lost the ticket and picks a seat randomly. But after that, the rest of passengers will:</p>

Expand Down Expand Up @@ -44,9 +44,9 @@
</ul>

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

### Hints
<details>
Expand Down
60 changes: 60 additions & 0 deletions problems/circular-permutation-in-binary-representation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](https://github.com/openset/leetcode/tree/master/problems/find-positive-integer-solution-for-a-given-equation "Find Positive Integer Solution for a Given Equation")

[Next >](https://github.com/openset/leetcode/tree/master/problems/maximum-length-of-a-concatenated-string-with-unique-characters "Maximum Length of a Concatenated String with Unique Characters")

## [1238. Circular Permutation in Binary Representation (Medium)](https://leetcode.com/problems/circular-permutation-in-binary-representation "循环码排列")

<p>Given 2 integers <code>n</code> and <code>start</code>. Your task is return <strong>any</strong> permutation <code>p</code>&nbsp;of <code>(0,1,2.....,2^n -1) </code>such that :</p>

<ul>
<li><code>p[0] = start</code></li>
<li><code>p[i]</code> and <code>p[i+1]</code>&nbsp;differ by only one bit in their binary representation.</li>
<li><code>p[0]</code> and <code>p[2^n -1]</code>&nbsp;must also differ by only one bit in their binary representation.</li>
</ul>

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

<pre>
<strong>Input:</strong> n = 2, start = 3
<strong>Output:</strong> [3,2,0,1]
<strong>Explanation:</strong> The binary representation of the permutation is (11,10,00,01).
All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]
</pre>

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

<pre>
<strong>Input:</strong> n = 3, start = 2
<strong>Output:</strong> [2,6,7,5,4,0,1,3]
<strong>Explanation:</strong> The binary representation of the permutation is (010,110,111,101,100,000,001,011).
</pre>

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

<ul>
<li><code>1 &lt;= n &lt;= 16</code></li>
<li><code>0 &lt;= start&nbsp;&lt;&nbsp;2 ^ n</code></li>
</ul>

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

### Hints
<details>
<summary>Hint 1</summary>
Use gray code to generate a n-bit sequence.
</details>

<details>
<summary>Hint 2</summary>
Rotate the sequence such that its first element is start.
</details>
17 changes: 16 additions & 1 deletion problems/duplicate-zeros/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,20 @@
### Hints
<details>
<summary>Hint 1</summary>
Iterate through the array backwards. You know whether an integer should be written or not based on how many zeroes are remaining in the array.
This is a great introductory problem for understanding and working with the concept of in-place operations. The problem statement clearly states that we are to modify the array in-place. That does not mean we cannot use another array. We just don't have to return anything.
</details>

<details>
<summary>Hint 2</summary>
A better way to solve this would be without using additional space. The only reason the problem statement allows you to make modifications in place is that it hints at avoiding any additional memory.
</details>

<details>
<summary>Hint 3</summary>
The main problem with not using additional memory is that we might override elements due to the zero duplication requirement of the problem statement. How do we get around that?
</details>

<details>
<summary>Hint 4</summary>
If we had enough space available, we would be able to accommodate all the elements properly. The new length would be the original length of the array plus the number of zeros. Can we use this information somehow to solve the problem?
</details>
16 changes: 16 additions & 0 deletions problems/find-all-numbers-disappeared-in-an-array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,19 @@
### Similar Questions
1. [First Missing Positive](https://github.com/openset/leetcode/tree/master/problems/first-missing-positive) (Hard)
1. [Find All Duplicates in an Array](https://github.com/openset/leetcode/tree/master/problems/find-all-duplicates-in-an-array) (Medium)

### Hints
<details>
<summary>Hint 1</summary>
This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think <b>counters!</b>
</details>

<details>
<summary>Hint 2</summary>
However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the solution. However, there's an interesting piece of information in this problem that makes it easy to re-use the input array itself for the solution.
</details>

<details>
<summary>Hint 3</summary>
The problem specifies that the numbers in the array will be in the range [1, n] where n is the number of elements in the array. Can we use this information and modify the array in-place somehow to find what we need?
</details>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](https://github.com/openset/leetcode/tree/master/problems/web-crawler "Web Crawler")

[Next >](https://github.com/openset/leetcode/tree/master/problems/circular-permutation-in-binary-representation "Circular Permutation in Binary Representation")

## [1237. Find Positive Integer Solution for a Given Equation (Easy)](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation "找出给定方程的正整数解")

<p>Given a&nbsp;function&nbsp; <code>f(x, y)</code>&nbsp;and a value <code>z</code>, return all positive integer&nbsp;pairs <code>x</code> and <code>y</code> where <code>f(x,y) == z</code>.</p>

<p>The function is constantly increasing, i.e.:</p>

<ul>
<li><code>f(x, y) &lt; f(x + 1, y)</code></li>
<li><code>f(x, y) &lt; f(x, y + 1)</code></li>
</ul>

<p>The function interface is defined like this:&nbsp;</p>

<pre>
interface CustomFunction {
public:
&nbsp; // Returns positive integer f(x, y) for any given positive integer x and y.
&nbsp; int f(int x, int y);
};
</pre>

<p>For custom testing purposes you&#39;re given an integer <code>function_id</code> and a target <code>z</code> as input, where <code>function_id</code> represent one function from an secret internal list, on the examples you&#39;ll know only two functions from the list. &nbsp;</p>

<p>You may return the solutions in any order.</p>

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

<pre>
<strong>Input:</strong> function_id = 1, z = 5
<strong>Output:</strong> [[1,4],[2,3],[3,2],[4,1]]
<strong>Explanation:</strong>&nbsp;function_id = 1 means that f(x, y) = x + y</pre>

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

<pre>
<strong>Input:</strong> function_id = 2, z = 5
<strong>Output:</strong> [[1,5],[5,1]]
<strong>Explanation:</strong>&nbsp;function_id = 2 means that f(x, y) = x * y
</pre>

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

<ul>
<li><code>1 &lt;= function_id &lt;= 9</code></li>
<li><code>1 &lt;= z &lt;= 100</code></li>
<li>It&#39;s guaranteed that the solutions of <code>f(x, y) == z</code> will be on the range <code>1 &lt;= x, y &lt;= 1000</code></li>
<li>It&#39;s also guaranteed that <code>f(x, y)</code> will fit in 32 bit signed integer if <code>1 &lt;= x, y &lt;= 1000</code></li>
</ul>

### Related Topics
[[Math](https://github.com/openset/leetcode/tree/master/tag/math/README.md)]
[[Binary Search](https://github.com/openset/leetcode/tree/master/tag/binary-search/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Loop over 1 ≤ x,y ≤ 1000 and check if f(x,y) == z.
</details>
14 changes: 14 additions & 0 deletions problems/find-the-celebrity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@

### Similar Questions
1. [Find the Town Judge](https://github.com/openset/leetcode/tree/master/problems/find-the-town-judge) (Easy)

### Hints
<details>
<summary>Hint 1</summary>
The best hint for this problem can be provided by the following figure:

<br>
<img src="https://assets.leetcode.com/uploads/2019/10/20/hint_find_celebrity.png" width="700"/>
</details>

<details>
<summary>Hint 2</summary>
Well, if you understood the gist of the above idea, you can extend it to find a candidate that can possibly be a celebrity. Why do we say a "candidate"? That is for you to think. This is clearly a greedy approach to find the answer. However, there is some information that would still remain to be verified without which we can't obtain an answer with certainty. To get that stake in the ground, we would need some more calls to the knows API.
</details>
21 changes: 21 additions & 0 deletions problems/max-consecutive-ones-iii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,24 @@ Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
1. [Longest Repeating Character Replacement](https://github.com/openset/leetcode/tree/master/problems/longest-repeating-character-replacement) (Medium)
1. [Max Consecutive Ones](https://github.com/openset/leetcode/tree/master/problems/max-consecutive-ones) (Easy)
1. [Max Consecutive Ones II](https://github.com/openset/leetcode/tree/master/problems/max-consecutive-ones-ii) (Medium)

### Hints
<details>
<summary>Hint 1</summary>
One thing's for sure, we will only flip a zero if it extends an existing window of 1s. Otherwise, there's no point in doing it, right? Think Sliding Window!
</details>

<details>
<summary>Hint 2</summary>
Since we know this problem can be solved using the sliding window construct, we might as well focus in that direction for hints. Basically, in a given window, we can never have > K zeros, right?
</details>

<details>
<summary>Hint 3</summary>
We don't have a fixed size window in this case. The window size can grow and shrink depending upon the number of zeros we have (we don't actually have to flip the zeros here!).
</details>

<details>
<summary>Hint 4</summary>
The way to shrink or expand a window would be based on the number of zeros that can still be flipped and so on.
</details>
8 changes: 8 additions & 0 deletions problems/max-consecutive-ones/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@
### Similar Questions
1. [Max Consecutive Ones II](https://github.com/openset/leetcode/tree/master/problems/max-consecutive-ones-ii) (Medium)
1. [Max Consecutive Ones III](https://github.com/openset/leetcode/tree/master/problems/max-consecutive-ones-iii) (Medium)

### Hints
<details>
<summary>Hint 1</summary>
You need to think about two things as far as any window is concerned. One is the starting point for the window. How do you detect that a new window of 1s has started? The next part is detecting the ending point for this window.

How do you detect the ending point for an existing window? If you figure these two things out, you will be able to detect the windows of consecutive ones. All that remains afterward is to find the longest such window and return the size.
</details>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author openset <[email protected]> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](https://github.com/openset/leetcode/tree/master/problems/circular-permutation-in-binary-representation "Circular Permutation in Binary Representation")

[Next >](https://github.com/openset/leetcode/tree/master/problems/tiling-a-rectangle-with-the-fewest-squares "Tiling a Rectangle with the Fewest Squares")

## [1239. Maximum Length of a Concatenated String with Unique Characters (Medium)](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters "串联字符串的最大长度")

<p>Given an array of strings <code>arr</code>. String <code>s</code> is a concatenation of a sub-sequence of <code>arr</code> which have <strong>unique characters</strong>.</p>

<p>Return <em>the maximum possible length</em> of <code>s</code>.</p>

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

<pre>
<strong>Input:</strong> arr = [&quot;un&quot;,&quot;iq&quot;,&quot;ue&quot;]
<strong>Output:</strong> 4
<strong>Explanation:</strong> All possible concatenations are &quot;&quot;,&quot;un&quot;,&quot;iq&quot;,&quot;ue&quot;,&quot;uniq&quot; and &quot;ique&quot;.
Maximum length is 4.
</pre>

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

<pre>
<strong>Input:</strong> arr = [&quot;cha&quot;,&quot;r&quot;,&quot;act&quot;,&quot;ers&quot;]
<strong>Output:</strong> 6
<strong>Explanation:</strong> Possible solutions are &quot;chaers&quot; and &quot;acters&quot;.
</pre>

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

<pre>
<strong>Input:</strong> arr = [&quot;abcdefghijklmnopqrstuvwxyz&quot;]
<strong>Output:</strong> 26
</pre>

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

<ul>
<li><code>1 &lt;= arr.length &lt;= 16</code></li>
<li><code>1 &lt;= arr[i].length &lt;= 26</code></li>
<li><code>arr[i]</code> contains only lower case English letters.</li>
</ul>

### Related Topics
[[Bit Manipulation](https://github.com/openset/leetcode/tree/master/tag/bit-manipulation/README.md)]
[[Backtracking](https://github.com/openset/leetcode/tree/master/tag/backtracking/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
You can try all combinations and keep mask of characters you have.
</details>

<details>
<summary>Hint 2</summary>
You can use DP.
</details>
Loading

0 comments on commit 582ff1d

Please sign in to comment.