Skip to content

Add: new #769

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

| # | Title | Solution | Difficulty |
| :-: | - | - | :-: |
| <span id="1383">1383</span> | [Maximum Performance of a Team](https://leetcode.com/problems/maximum-performance-of-a-team "最大的团队表现值") | [Go](problems/maximum-performance-of-a-team) | Hard |
| <span id="1382">1382</span> | [Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree "将二叉搜索树变平衡") | [Go](problems/balance-a-binary-search-tree) | Medium |
| <span id="1381">1381</span> | [Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation "设计一个支持增量操作的栈") | [Go](problems/design-a-stack-with-increment-operation) | Medium |
| <span id="1380">1380</span> | [Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix "矩阵中的幸运数") | [Go](problems/lucky-numbers-in-a-matrix) | Easy |
| <span id="1379">1379</span> | [Find a Corresponding Node of a Binary Tree in a Clone of That Tree](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree "找出克隆二叉树中的相同节点") | [Go](problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree) | Medium |
| <span id="1378">1378</span> | [Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier "使用唯一标识码替换员工ID") 🔒 | [MySQL](problems/replace-employee-id-with-the-unique-identifier) | Easy |
| <span id="1377">1377</span> | [Frog Position After T Seconds](https://leetcode.com/problems/frog-position-after-t-seconds "T 秒后青蛙的位置") | [Go](problems/frog-position-after-t-seconds) | Hard |
| <span id="1376">1376</span> | [Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees "通知所有员工所需的时间") | [Go](problems/time-needed-to-inform-all-employees) | Medium |
| <span id="1375">1375</span> | [Bulb Switcher III](https://leetcode.com/problems/bulb-switcher-iii "灯泡开关 III") | [Go](problems/bulb-switcher-iii) | Medium |
Expand Down
51 changes: 51 additions & 0 deletions problems/balance-a-binary-search-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!--|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](../design-a-stack-with-increment-operation "Design a Stack With Increment Operation")

[Next >](../maximum-performance-of-a-team "Maximum Performance of a Team")

## [1382. Balance a Binary Search Tree (Medium)](https://leetcode.com/problems/balance-a-binary-search-tree "将二叉搜索树变平衡")

<p>Given a binary search tree, return a <strong>balanced</strong> binary search tree with the same node values.</p>

<p>A binary search tree is <em>balanced</em> if and only if&nbsp;the depth of the two subtrees of&nbsp;every&nbsp;node never differ by more than 1.</p>

<p>If there is more than one answer, return any of them.</p>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/08/22/1515_ex1.png" style="width: 250px; height: 248px;" /><img alt="" src="https://assets.leetcode.com/uploads/2019/08/22/1515_ex1_out.png" style="width: 200px; height: 200px;" /></strong></p>

<pre>
<strong>Input:</strong> root = [1,null,2,null,3,null,4,null,null]
<strong>Output:</strong> [2,1,3,null,null,null,4]
<b>Explanation:</b> This is not the only correct answer, [3,1,4,null,2,null,null] is also correct.
</pre>

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

<ul>
<li>The number of nodes in the tree is between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>10^4</code>.</li>
<li>The tree nodes will have distinct values between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>10^5</code>.</li>
</ul>

### Related Topics
[[Binary Search Tree](../../tag/binary-search-tree/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Convert the tree to a sorted array using an in-order traversal.
</details>

<details>
<summary>Hint 2</summary>
Construct a new balanced tree from the sorted array recursively.
</details>
22 changes: 13 additions & 9 deletions problems/course-schedule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,40 @@

## [207. Course Schedule (Medium)](https://leetcode.com/problems/course-schedule "课程表")

<p>There are a total of <i>n</i> courses you have to take, labeled from <code>0</code> to <code>n-1</code>.</p>
<p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses-1</code>.</p>

<p>Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: <code>[0,1]</code></p>

<p>Given the total number of courses and a list of prerequisite <b>pairs</b>, is it possible for you to finish all courses?</p>

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

<pre>
<strong>Input:</strong> 2, [[1,0]]
<strong>Output: </strong>true
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]]
<strong>Output:</strong> true
<strong>Explanation:</strong>&nbsp;There are a total of 2 courses to take.
&nbsp; To take course 1 you should have finished course 0. So it is possible.</pre>
&nbsp; To take course 1 you should have finished course 0. So it is possible.
</pre>

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

<pre>
<strong>Input:</strong> 2, [[1,0],[0,1]]
<strong>Output: </strong>false
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0],[0,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong>&nbsp;There are a total of 2 courses to take.
&nbsp; To take course 1 you should have finished course 0, and to take course 0 you should
&nbsp; also have finished course 1. So it is impossible.
</pre>

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

<ol>
<ul>
<li>The input prerequisites is a graph represented by <b>a list of edges</b>, not adjacency matrices. Read more about <a href="https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs" target="_blank">how a graph is represented</a>.</li>
<li>You may assume that there are no duplicate edges in the input prerequisites.</li>
</ol>
<li><code>1 &lt;=&nbsp;numCourses &lt;= 10^5</code></li>
</ul>

### Related Topics
[[Depth-first Search](../../tag/depth-first-search/README.md)]
Expand Down
9 changes: 7 additions & 2 deletions problems/cut-off-trees-for-golf-event/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<li><code>The place with number bigger than 1</code> represents a <code>tree</code> can be walked through, and this positive number represents the tree&#39;s height.</li>
</ol>

<p>&nbsp;</p>
<p>In one step you can walk in any of the four directions <code>top</code>, <code>bottom</code>, <code>left</code> and <code>right</code>&nbsp;also when standing in a point which is a tree you can decide whether or not to cut off the tree.</p>

<p>You are asked to cut off <b>all</b> the trees in this forest in the order of tree&#39;s height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).</p>

Expand Down Expand Up @@ -69,8 +69,13 @@
</pre>

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

<p><b>Hint</b>: size of the given matrix will not exceed 50x50.</p>
<ul>
<li><code>1 &lt;= forest.length &lt;= 50</code></li>
<li><code>1 &lt;= forest[i].length &lt;= 50</code></li>
<li><code>0 &lt;= forest[i][j]&nbsp;&lt;= 10^9</code></li>
</ul>

### Related Topics
[[Breadth-first Search](../../tag/breadth-first-search/README.md)]
74 changes: 74 additions & 0 deletions problems/design-a-stack-with-increment-operation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!--|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](../lucky-numbers-in-a-matrix "Lucky Numbers in a Matrix")

[Next >](../balance-a-binary-search-tree "Balance a Binary Search Tree")

## [1381. Design a Stack With Increment Operation (Medium)](https://leetcode.com/problems/design-a-stack-with-increment-operation "设计一个支持增量操作的栈")

<p>Design a stack which supports the following operations.</p>

<p>Implement the <code>CustomStack</code> class:</p>

<ul>
<li><code>CustomStack(int maxSize)</code> Initializes the object with <code>maxSize</code> which is the maximum number of elements in the stack or do nothing if the stack reached the <code>maxSize</code>.</li>
<li><code>void push(int x)</code>&nbsp;Adds <code>x</code> to the top of the stack if the stack hasn&#39;t reached the <code>maxSize</code>.</li>
<li><code>int pop()</code>&nbsp;Pops and returns the top of stack or <strong>-1</strong> if the stack is empty.</li>
<li><code>void inc(int k, int val)</code> Increments the bottom <code>k</code> elements of the stack by <code>val</code>. If there are less than <code>k</code> elements in the stack, just increment all the elements in the stack.</li>
</ul>

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

<pre>
<strong>Input</strong>
[&quot;CustomStack&quot;,&quot;push&quot;,&quot;push&quot;,&quot;pop&quot;,&quot;push&quot;,&quot;push&quot;,&quot;push&quot;,&quot;increment&quot;,&quot;increment&quot;,&quot;pop&quot;,&quot;pop&quot;,&quot;pop&quot;,&quot;pop&quot;]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
<strong>Output</strong>
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
<strong>Explanation</strong>
CustomStack customStack = new CustomStack(3); // Stack is Empty []
customStack.push(1); // stack becomes [1]
customStack.push(2); // stack becomes [1, 2]
customStack.pop(); // return 2 --&gt; Return top of the stack 2, stack becomes [1]
customStack.push(2); // stack becomes [1, 2]
customStack.push(3); // stack becomes [1, 2, 3]
customStack.push(4); // stack still [1, 2, 3], Don&#39;t add another elements as size is 4
customStack.increment(5, 100); // stack becomes [101, 102, 103]
customStack.increment(2, 100); // stack becomes [201, 202, 103]
customStack.pop(); // return 103 --&gt; Return top of the stack 103, stack becomes [201, 202]
customStack.pop(); // return 202 --&gt; Return top of the stack 102, stack becomes [201]
customStack.pop(); // return 201 --&gt; Return top of the stack 101, stack becomes []
customStack.pop(); // return -1 --&gt; Stack is empty return -1.
</pre>

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

<ul>
<li><code>1 &lt;= maxSize &lt;= 1000</code></li>
<li><code>1 &lt;= x &lt;= 1000</code></li>
<li><code>1 &lt;= k &lt;= 1000</code></li>
<li><code>0 &lt;= val &lt;= 100</code></li>
<li>At most&nbsp;<code>1000</code>&nbsp;calls will be made to each method of <code>increment</code>, <code>push</code> and <code>pop</code> each separately.</li>
</ul>

### Related Topics
[[Stack](../../tag/stack/README.md)]
[[Design](../../tag/design/README.md)]

### Hints
<details>
<summary>Hint 1</summary>
Use an array to represent the stack. Push will add new integer to the array. Pop removes the last element in the array and increment will add val to the first k elements of the array.
</details>

<details>
<summary>Hint 2</summary>
This solution run in O(1) per push and pop and O(k) per increment.
</details>
12 changes: 8 additions & 4 deletions problems/divide-two-integers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@

<p>Return the quotient after dividing <code>dividend</code> by <code>divisor</code>.</p>

<p>The integer division should truncate toward zero.</p>
<p>The integer division should truncate toward zero, which means losing its fractional part. For example, <code>truncate(8.345) = 8</code> and <code>truncate(-2.7335) = -2</code>.</p>

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

<pre>
<strong>Input:</strong> dividend = 10, divisor = 3
<strong>Output:</strong> 3</pre>
<strong>Output:</strong> 3
<strong>Explanation:</strong> 10/3 = truncate(3.33333..) = truncate(3) = 3.
</pre>

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

<pre>
<strong>Input:</strong> dividend = 7, divisor = -3
<strong>Output:</strong> -2</pre>
<strong>Output:</strong> -2
<strong>Explanation:</strong> 7/-3 = truncate(-2.33333..) = truncate(-2) = 3.
</pre>

<p><strong>Note:</strong></p>

<ul>
<li>Both dividend and divisor&nbsp;will be&nbsp;32-bit&nbsp;signed integers.</li>
<li>The divisor will never be 0.</li>
<li>Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [&minus;2<sup>31</sup>, &nbsp;2<sup>31</sup> &minus; 1]. For the purpose of this problem, assume that your function returns 2<sup>31</sup> &minus; 1 when the division result&nbsp;overflows.</li>
<li>Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [&minus;2<sup>31</sup>, &nbsp;2<sup>31</sup> &minus; 1]. For the purpose of this problem, assume that your function <strong>returns 2<sup>31</sup> &minus; 1 when the division result&nbsp;overflows</strong>.</li>
</ul>

### Related Topics
Expand Down
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 <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](../replace-employee-id-with-the-unique-identifier "Replace Employee ID With The Unique Identifier")

[Next >](../lucky-numbers-in-a-matrix "Lucky Numbers in a Matrix")

## [1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree (Medium)](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree "找出克隆二叉树中的相同节点")

<p>Given two binary trees <code>original</code> and <code>cloned</code> and given a reference to a node <code>target</code> in the original tree.</p>

<p>The <code>cloned</code> tree is a <strong>copy of</strong> the <code>original</code> tree.</p>

<p>Return <em>a reference to the same node</em> in the <code>cloned</code> tree.</p>

<p><strong>Note</strong> that you are <strong>not allowed</strong> to change any of the two trees or the <code>target</code> node and the answer <strong>must be</strong> a reference to a node in the <code>cloned</code> tree.</p>

<p><strong>Follow up:</strong>&nbsp;Solve the problem if repeated values on the tree are allowed.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e1.png" style="width: 544px; height: 426px;" />
<pre>
<strong>Input:</strong> tree = [7,4,3,null,null,6,19], target = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree.
</pre>

<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e2.png" style="width: 221px; height: 159px;" />
<pre>
<strong>Input:</strong> tree = [7], target = 7
<strong>Output:</strong> 7
</pre>

<p><strong>Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e3.png" style="width: 459px; height: 486px;" />
<pre>
<strong>Input:</strong> tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4
<strong>Output:</strong> 4
</pre>

<p><strong>Example 4:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e4.png" style="width: 555px; height: 239px;" />
<pre>
<strong>Input:</strong> tree = [1,2,3,4,5,6,7,8,9,10], target = 5
<strong>Output:</strong> 5
</pre>

<p><strong>Example 5:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e5.png" style="width: 427px; height: 345px;" />
<pre>
<strong>Input:</strong> tree = [1,2,null,3], target = 2
<strong>Output:</strong> 2
</pre>

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

<ul>
<li>The number of nodes in the <code>tree</code> is in the range <code>[1, 10^4]</code>.</li>
<li>The values of the nodes of the <code>tree</code> are unique.</li>
<li><code>target</code> node is a&nbsp;node from the <code>original</code> tree and is not <code>null</code>.</li>
</ul>

### Related Topics
[[Tree](../../tag/tree/README.md)]
2 changes: 1 addition & 1 deletion problems/frog-position-after-t-seconds/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[< Previous](../time-needed-to-inform-all-employees "Time Needed to Inform All Employees")

Next >
[Next >](../replace-employee-id-with-the-unique-identifier "Replace Employee ID With The Unique Identifier")

## [1377. Frog Position After T Seconds (Hard)](https://leetcode.com/problems/frog-position-after-t-seconds "T 秒后青蛙的位置")

Expand Down
2 changes: 1 addition & 1 deletion problems/guess-number-higher-or-lower-ii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ You end up paying $5 + $7 + $9 = $21.
<p>Given a particular <strong>n &ge; 1</strong>, find out how much money you need to have to guarantee a <b>win</b>.</p>

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

### Similar Questions
1. [Flip Game II](../flip-game-ii) (Medium)
Expand Down
12 changes: 6 additions & 6 deletions problems/increasing-order-search-tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
&nbsp; 8
&nbsp; \
9 </pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<p><strong>Note:</strong></p>

<ol>
<li>The number of nodes in the given tree will be between 1 and 100.</li>
<li>Each node will have a unique integer value from 0 to 1000.</li>
</ol>
<ul>
<li>The number of nodes in the given tree will be between <code>1</code> and <code>100</code>.</li>
<li>Each node will have a unique integer value from <code>0</code> to <code>1000</code>.</li>
</ul>

### Related Topics
[[Tree](../../tag/tree/README.md)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ collection.getRandom();
</p>

### Related Topics
[[Design](../../tag/design/README.md)]
[[Array](../../tag/array/README.md)]
[[Hash Table](../../tag/hash-table/README.md)]
[[Design](../../tag/design/README.md)]

### Similar Questions
1. [Insert Delete GetRandom O(1)](../insert-delete-getrandom-o1) (Medium)
2 changes: 1 addition & 1 deletion problems/insert-delete-getrandom-o1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ randomSet.getRandom();
</p>

### Related Topics
[[Design](../../tag/design/README.md)]
[[Array](../../tag/array/README.md)]
[[Hash Table](../../tag/hash-table/README.md)]
[[Design](../../tag/design/README.md)]

### Similar Questions
1. [Insert Delete GetRandom O(1) - Duplicates allowed](../insert-delete-getrandom-o1-duplicates-allowed) (Hard)
Loading