Skip to content

Commit 70a6508

Browse files
author
Shuo
committed
Add: new
1 parent 545dd88 commit 70a6508

File tree

26 files changed

+437
-51
lines changed

26 files changed

+437
-51
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ LeetCode Problems' Solutions
6262

6363
| # | Title | Solution | Difficulty |
6464
| :-: | - | - | :-: |
65+
| <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 |
66+
| <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 |
67+
| <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 |
68+
| <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 |
69+
| <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 |
70+
| <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 |
6571
| <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 |
6672
| <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 |
6773
| <span id="1375">1375</span> | [Bulb Switcher III](https://leetcode.com/problems/bulb-switcher-iii "灯泡开关 III") | [Go](problems/bulb-switcher-iii) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../design-a-stack-with-increment-operation "Design a Stack With Increment Operation")
9+
                
10+
[Next >](../maximum-performance-of-a-team "Maximum Performance of a Team")
11+
12+
## [1382. Balance a Binary Search Tree (Medium)](https://leetcode.com/problems/balance-a-binary-search-tree "将二叉搜索树变平衡")
13+
14+
<p>Given a binary search tree, return a <strong>balanced</strong> binary search tree with the same node values.</p>
15+
16+
<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>
17+
18+
<p>If there is more than one answer, return any of them.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Example 1:</strong></p>
22+
23+
<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>
24+
25+
<pre>
26+
<strong>Input:</strong> root = [1,null,2,null,3,null,4,null,null]
27+
<strong>Output:</strong> [2,1,3,null,null,null,4]
28+
<b>Explanation:</b> This is not the only correct answer, [3,1,4,null,2,null,null] is also correct.
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li>The number of nodes in the tree is between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>10^4</code>.</li>
36+
<li>The tree nodes will have distinct values between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>10^5</code>.</li>
37+
</ul>
38+
39+
### Related Topics
40+
[[Binary Search Tree](../../tag/binary-search-tree/README.md)]
41+
42+
### Hints
43+
<details>
44+
<summary>Hint 1</summary>
45+
Convert the tree to a sorted array using an in-order traversal.
46+
</details>
47+
48+
<details>
49+
<summary>Hint 2</summary>
50+
Construct a new balanced tree from the sorted array recursively.
51+
</details>

problems/course-schedule/README.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,40 @@
1111

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

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

1616
<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>
1717

1818
<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>
1919

20+
<p>&nbsp;</p>
2021
<p><strong>Example 1:</strong></p>
2122

2223
<pre>
23-
<strong>Input:</strong> 2, [[1,0]]
24-
<strong>Output: </strong>true
24+
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]]
25+
<strong>Output:</strong> true
2526
<strong>Explanation:</strong>&nbsp;There are a total of 2 courses to take.
26-
&nbsp; To take course 1 you should have finished course 0. So it is possible.</pre>
27+
&nbsp; To take course 1 you should have finished course 0. So it is possible.
28+
</pre>
2729

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

3032
<pre>
31-
<strong>Input:</strong> 2, [[1,0],[0,1]]
32-
<strong>Output: </strong>false
33+
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0],[0,1]]
34+
<strong>Output:</strong> false
3335
<strong>Explanation:</strong>&nbsp;There are a total of 2 courses to take.
3436
&nbsp; To take course 1 you should have finished course 0, and to take course 0 you should
3537
&nbsp; also have finished course 1. So it is impossible.
3638
</pre>
3739

38-
<p><b>Note:</b></p>
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
3942

40-
<ol>
43+
<ul>
4144
<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>
4245
<li>You may assume that there are no duplicate edges in the input prerequisites.</li>
43-
</ol>
46+
<li><code>1 &lt;=&nbsp;numCourses &lt;= 10^5</code></li>
47+
</ul>
4448

4549
### Related Topics
4650
[[Depth-first Search](../../tag/depth-first-search/README.md)]

problems/cut-off-trees-for-golf-event/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<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>
2020
</ol>
2121

22-
<p>&nbsp;</p>
22+
<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>
2323

2424
<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>
2525

@@ -69,8 +69,13 @@
6969
</pre>
7070

7171
<p>&nbsp;</p>
72+
<p><strong>Constraints:</strong></p>
7273

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

7580
### Related Topics
7681
[[Breadth-first Search](../../tag/breadth-first-search/README.md)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../lucky-numbers-in-a-matrix "Lucky Numbers in a Matrix")
9+
                
10+
[Next >](../balance-a-binary-search-tree "Balance a Binary Search Tree")
11+
12+
## [1381. Design a Stack With Increment Operation (Medium)](https://leetcode.com/problems/design-a-stack-with-increment-operation "设计一个支持增量操作的栈")
13+
14+
<p>Design a stack which supports the following operations.</p>
15+
16+
<p>Implement the <code>CustomStack</code> class:</p>
17+
18+
<ul>
19+
<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>
20+
<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>
21+
<li><code>int pop()</code>&nbsp;Pops and returns the top of stack or <strong>-1</strong> if the stack is empty.</li>
22+
<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>
23+
</ul>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input</strong>
30+
[&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;]
31+
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
32+
<strong>Output</strong>
33+
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
34+
<strong>Explanation</strong>
35+
CustomStack customStack = new CustomStack(3); // Stack is Empty []
36+
customStack.push(1); // stack becomes [1]
37+
customStack.push(2); // stack becomes [1, 2]
38+
customStack.pop(); // return 2 --&gt; Return top of the stack 2, stack becomes [1]
39+
customStack.push(2); // stack becomes [1, 2]
40+
customStack.push(3); // stack becomes [1, 2, 3]
41+
customStack.push(4); // stack still [1, 2, 3], Don&#39;t add another elements as size is 4
42+
customStack.increment(5, 100); // stack becomes [101, 102, 103]
43+
customStack.increment(2, 100); // stack becomes [201, 202, 103]
44+
customStack.pop(); // return 103 --&gt; Return top of the stack 103, stack becomes [201, 202]
45+
customStack.pop(); // return 202 --&gt; Return top of the stack 102, stack becomes [201]
46+
customStack.pop(); // return 201 --&gt; Return top of the stack 101, stack becomes []
47+
customStack.pop(); // return -1 --&gt; Stack is empty return -1.
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= maxSize &lt;= 1000</code></li>
55+
<li><code>1 &lt;= x &lt;= 1000</code></li>
56+
<li><code>1 &lt;= k &lt;= 1000</code></li>
57+
<li><code>0 &lt;= val &lt;= 100</code></li>
58+
<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>
59+
</ul>
60+
61+
### Related Topics
62+
[[Stack](../../tag/stack/README.md)]
63+
[[Design](../../tag/design/README.md)]
64+
65+
### Hints
66+
<details>
67+
<summary>Hint 1</summary>
68+
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.
69+
</details>
70+
71+
<details>
72+
<summary>Hint 2</summary>
73+
This solution run in O(1) per push and pop and O(k) per increment.
74+
</details>

problems/divide-two-integers/README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,30 @@
1515

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

18-
<p>The integer division should truncate toward zero.</p>
18+
<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>
1919

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

2222
<pre>
2323
<strong>Input:</strong> dividend = 10, divisor = 3
24-
<strong>Output:</strong> 3</pre>
24+
<strong>Output:</strong> 3
25+
<strong>Explanation:</strong> 10/3 = truncate(3.33333..) = truncate(3) = 3.
26+
</pre>
2527

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

2830
<pre>
2931
<strong>Input:</strong> dividend = 7, divisor = -3
30-
<strong>Output:</strong> -2</pre>
32+
<strong>Output:</strong> -2
33+
<strong>Explanation:</strong> 7/-3 = truncate(-2.33333..) = truncate(-2) = 3.
34+
</pre>
3135

3236
<p><strong>Note:</strong></p>
3337

3438
<ul>
3539
<li>Both dividend and divisor&nbsp;will be&nbsp;32-bit&nbsp;signed integers.</li>
3640
<li>The divisor will never be 0.</li>
37-
<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>
41+
<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>
3842
</ul>
3943

4044
### Related Topics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../replace-employee-id-with-the-unique-identifier "Replace Employee ID With The Unique Identifier")
9+
                
10+
[Next >](../lucky-numbers-in-a-matrix "Lucky Numbers in a Matrix")
11+
12+
## [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 "找出克隆二叉树中的相同节点")
13+
14+
<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>
15+
16+
<p>The <code>cloned</code> tree is a <strong>copy of</strong> the <code>original</code> tree.</p>
17+
18+
<p>Return <em>a reference to the same node</em> in the <code>cloned</code> tree.</p>
19+
20+
<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>
21+
22+
<p><strong>Follow up:</strong>&nbsp;Solve the problem if repeated values on the tree are allowed.</p>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Example 1:</strong></p>
26+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e1.png" style="width: 544px; height: 426px;" />
27+
<pre>
28+
<strong>Input:</strong> tree = [7,4,3,null,null,6,19], target = 3
29+
<strong>Output:</strong> 3
30+
<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.
31+
</pre>
32+
33+
<p><strong>Example 2:</strong></p>
34+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e2.png" style="width: 221px; height: 159px;" />
35+
<pre>
36+
<strong>Input:</strong> tree = [7], target = 7
37+
<strong>Output:</strong> 7
38+
</pre>
39+
40+
<p><strong>Example 3:</strong></p>
41+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e3.png" style="width: 459px; height: 486px;" />
42+
<pre>
43+
<strong>Input:</strong> tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4
44+
<strong>Output:</strong> 4
45+
</pre>
46+
47+
<p><strong>Example 4:</strong></p>
48+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e4.png" style="width: 555px; height: 239px;" />
49+
<pre>
50+
<strong>Input:</strong> tree = [1,2,3,4,5,6,7,8,9,10], target = 5
51+
<strong>Output:</strong> 5
52+
</pre>
53+
54+
<p><strong>Example 5:</strong></p>
55+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/21/e5.png" style="width: 427px; height: 345px;" />
56+
<pre>
57+
<strong>Input:</strong> tree = [1,2,null,3], target = 2
58+
<strong>Output:</strong> 2
59+
</pre>
60+
61+
<p>&nbsp;</p>
62+
<p><strong>Constraints:</strong></p>
63+
64+
<ul>
65+
<li>The number of nodes in the <code>tree</code> is in the range <code>[1, 10^4]</code>.</li>
66+
<li>The values of the nodes of the <code>tree</code> are unique.</li>
67+
<li><code>target</code> node is a&nbsp;node from the <code>original</code> tree and is not <code>null</code>.</li>
68+
</ul>
69+
70+
### Related Topics
71+
[[Tree](../../tag/tree/README.md)]

problems/frog-position-after-t-seconds/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

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

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

problems/guess-number-higher-or-lower-ii/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ You end up paying $5 + $7 + $9 = $21.
3636
<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>
3737

3838
### Related Topics
39-
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
4039
[[Minimax](../../tag/minimax/README.md)]
40+
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
4141

4242
### Similar Questions
4343
1. [Flip Game II](../flip-game-ii) (Medium)

problems/increasing-order-search-tree/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
&nbsp; 8
4545
&nbsp; \
4646
9 </pre>
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
4749

48-
<p><strong>Note:</strong></p>
49-
50-
<ol>
51-
<li>The number of nodes in the given tree will be between 1 and 100.</li>
52-
<li>Each node will have a unique integer value from 0 to 1000.</li>
53-
</ol>
50+
<ul>
51+
<li>The number of nodes in the given tree will be between <code>1</code> and <code>100</code>.</li>
52+
<li>Each node will have a unique integer value from <code>0</code> to <code>1000</code>.</li>
53+
</ul>
5454

5555
### Related Topics
5656
[[Tree](../../tag/tree/README.md)]

problems/insert-delete-getrandom-o1-duplicates-allowed/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ collection.getRandom();
4747
</p>
4848

4949
### Related Topics
50-
[[Design](../../tag/design/README.md)]
5150
[[Array](../../tag/array/README.md)]
5251
[[Hash Table](../../tag/hash-table/README.md)]
52+
[[Design](../../tag/design/README.md)]
5353

5454
### Similar Questions
5555
1. [Insert Delete GetRandom O(1)](../insert-delete-getrandom-o1) (Medium)

problems/insert-delete-getrandom-o1/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ randomSet.getRandom();
5050
</p>
5151

5252
### Related Topics
53-
[[Design](../../tag/design/README.md)]
5453
[[Array](../../tag/array/README.md)]
5554
[[Hash Table](../../tag/hash-table/README.md)]
55+
[[Design](../../tag/design/README.md)]
5656

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

0 commit comments

Comments
 (0)