Skip to content

Commit 964bea7

Browse files
committed
add 1600-1799 readme
1 parent 0be0b10 commit 964bea7

File tree

244 files changed

+20378
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

244 files changed

+20378
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [1600. 皇位继承顺序](https://leetcode-cn.com/problems/throne-inheritance)
2+
3+
[English Version](/solution/1500-1599/1600.Throne%20Inheritance/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
<p>一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。</p>
9+
10+
<p>这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数&nbsp;<code>Successor(x, curOrder)</code>&nbsp;,给定一个人&nbsp;<code>x</code>&nbsp;和当前的继承顺序,该函数返回 <code>x</code>&nbsp;的下一继承人。</p>
11+
12+
<pre>Successor(x, curOrder):
13+
如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中:
14+
如果 x 是国王,那么返回 null
15+
否则,返回 Successor(x 的父亲, curOrder)
16+
否则,返回 x 不在 curOrder 中最年长的孩子
17+
</pre>
18+
19+
<p>比方说,假设王国由国王,他的孩子&nbsp;Alice 和 Bob (Alice 比 Bob&nbsp;年长)和 Alice 的孩子&nbsp;Jack 组成。</p>
20+
21+
<ol>
22+
<li>一开始,&nbsp;<code>curOrder</code>&nbsp;为&nbsp;<code>[&quot;king&quot;]</code>.</li>
23+
<li>调用&nbsp;<code>Successor(king, curOrder)</code>&nbsp;,返回 Alice ,所以我们将 Alice 放入 <code>curOrder</code>&nbsp;中,得到&nbsp;<code>[&quot;king&quot;, &quot;Alice&quot;]</code>&nbsp;。</li>
24+
<li>调用&nbsp;<code>Successor(Alice, curOrder)</code>&nbsp;,返回 Jack ,所以我们将 Jack 放入&nbsp;<code>curOrder</code>&nbsp;中,得到&nbsp;<code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;]</code>&nbsp;。</li>
25+
<li>调用&nbsp;<code>Successor(Jack, curOrder)</code>&nbsp;,返回 Bob ,所以我们将 Bob 放入&nbsp;<code>curOrder</code>&nbsp;中,得到&nbsp;<code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;, &quot;Bob&quot;]</code>&nbsp;。</li>
26+
<li>调用&nbsp;<code>Successor(Bob, curOrder)</code>&nbsp;,返回&nbsp;<code>null</code>&nbsp;。最终得到继承顺序为&nbsp;<code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;, &quot;Bob&quot;]</code>&nbsp;。</li>
27+
</ol>
28+
29+
<p>通过以上的函数,我们总是能得到一个唯一的继承顺序。</p>
30+
31+
<p>请你实现&nbsp;<code>ThroneInheritance</code>&nbsp;类:</p>
32+
33+
<ul>
34+
<li><code>ThroneInheritance(string kingName)</code> 初始化一个&nbsp;<code>ThroneInheritance</code>&nbsp;类的对象。国王的名字作为构造函数的参数传入。</li>
35+
<li><code>void birth(string parentName, string childName)</code>&nbsp;表示&nbsp;<code>parentName</code>&nbsp;新拥有了一个名为&nbsp;<code>childName</code>&nbsp;的孩子。</li>
36+
<li><code>void death(string name)</code>&nbsp;表示名为&nbsp;<code>name</code>&nbsp;的人死亡。一个人的死亡不会影响&nbsp;<code>Successor</code>&nbsp;函数,也不会影响当前的继承顺序。你可以只将这个人标记为死亡状态。</li>
37+
<li><code>string[] getInheritanceOrder()</code>&nbsp;返回 <strong>除去</strong>&nbsp;死亡人员的当前继承顺序列表。</li>
38+
</ul>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>示例:</strong></p>
43+
44+
<pre><strong>输入:</strong>
45+
[&quot;ThroneInheritance&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;getInheritanceOrder&quot;, &quot;death&quot;, &quot;getInheritanceOrder&quot;]
46+
[[&quot;king&quot;], [&quot;king&quot;, &quot;andy&quot;], [&quot;king&quot;, &quot;bob&quot;], [&quot;king&quot;, &quot;catherine&quot;], [&quot;andy&quot;, &quot;matthew&quot;], [&quot;bob&quot;, &quot;alex&quot;], [&quot;bob&quot;, &quot;asha&quot;], [null], [&quot;bob&quot;], [null]]
47+
<strong>输出:</strong>
48+
[null, null, null, null, null, null, null, [&quot;king&quot;, &quot;andy&quot;, &quot;matthew&quot;, &quot;bob&quot;, &quot;alex&quot;, &quot;asha&quot;, &quot;catherine&quot;], null, [&quot;king&quot;, &quot;andy&quot;, &quot;matthew&quot;, &quot;alex&quot;, &quot;asha&quot;, &quot;catherine&quot;]]
49+
50+
<strong>解释:</strong>
51+
ThroneInheritance t= new ThroneInheritance(&quot;king&quot;); // 继承顺序:<strong>king</strong>
52+
t.birth(&quot;king&quot;, &quot;andy&quot;); // 继承顺序:king &gt; <strong>andy</strong>
53+
t.birth(&quot;king&quot;, &quot;bob&quot;); // 继承顺序:king &gt; andy &gt; <strong>bob</strong>
54+
t.birth(&quot;king&quot;, &quot;catherine&quot;); // 继承顺序:king &gt; andy &gt; bob &gt; <strong>catherine</strong>
55+
t.birth(&quot;andy&quot;, &quot;matthew&quot;); // 继承顺序:king &gt; andy &gt; <strong>matthew</strong> &gt; bob &gt; catherine
56+
t.birth(&quot;bob&quot;, &quot;alex&quot;); // 继承顺序:king &gt; andy &gt; matthew &gt; bob &gt; <strong>alex</strong> &gt; catherine
57+
t.birth(&quot;bob&quot;, &quot;asha&quot;); // 继承顺序:king &gt; andy &gt; matthew &gt; bob &gt; alex &gt; <strong>asha</strong> &gt; catherine
58+
t.getInheritanceOrder(); // 返回 [&quot;king&quot;, &quot;andy&quot;, &quot;matthew&quot;, &quot;bob&quot;, &quot;alex&quot;, &quot;asha&quot;, &quot;catherine&quot;]
59+
t.death(&quot;bob&quot;); // 继承顺序:king &gt; andy &gt; matthew &gt; <strong>bob(已经去世)</strong>&gt; alex &gt; asha &gt; catherine
60+
t.getInheritanceOrder(); // 返回 [&quot;king&quot;, &quot;andy&quot;, &quot;matthew&quot;, &quot;alex&quot;, &quot;asha&quot;, &quot;catherine&quot;]
61+
</pre>
62+
63+
<p>&nbsp;</p>
64+
65+
<p><strong>提示:</strong></p>
66+
67+
<ul>
68+
<li><code>1 &lt;= kingName.length, parentName.length, childName.length, name.length &lt;= 15</code></li>
69+
<li><code>kingName</code>,<code>parentName</code>,&nbsp;<code>childName</code>&nbsp;和&nbsp;<code>name</code>&nbsp;仅包含小写英文字母。</li>
70+
<li>所有的参数&nbsp;<code>childName</code> 和&nbsp;<code>kingName</code>&nbsp;<strong>互不相同</strong>。</li>
71+
<li>所有&nbsp;<code>death</code>&nbsp;函数中的死亡名字 <code>name</code>&nbsp;要么是国王,要么是已经出生了的人员名字。</li>
72+
<li>每次调用 <code>birth(parentName, childName)</code> 时,测试用例都保证 <code>parentName</code> 对应的人员是活着的。</li>
73+
<li>最多调用&nbsp;<code>10<sup>5</sup></code>&nbsp;次<code>birth</code> 和&nbsp;<code>death</code>&nbsp;。</li>
74+
<li>最多调用&nbsp;<code>10</code>&nbsp;次&nbsp;<code>getInheritanceOrder</code>&nbsp;。</li>
75+
</ul>
76+
77+
78+
79+
## 解法
80+
81+
<!-- 这里可写通用的实现逻辑 -->
82+
83+
84+
<!-- tabs:start -->
85+
86+
### **Python3**
87+
88+
<!-- 这里可写当前语言的特殊实现逻辑 -->
89+
90+
```python
91+
92+
```
93+
94+
### **Java**
95+
96+
<!-- 这里可写当前语言的特殊实现逻辑 -->
97+
98+
```java
99+
100+
```
101+
102+
### **...**
103+
```
104+
105+
```
106+
107+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# [1600. Throne Inheritance](https://leetcode.com/problems/throne-inheritance)
2+
3+
[中文文档](/solution/1500-1599/1600.Throne%20Inheritance/README.md)
4+
5+
## Description
6+
7+
<p>A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born.</p>
8+
9+
<p>The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let&#39;s define the recursive function <code>Successor(x, curOrder)</code>, which given a person <code>x</code> and the inheritance order so far, returns who should be the next person after <code>x</code> in the order of inheritance.</p>
10+
11+
<pre>
12+
Successor(x, curOrder):
13+
if x has no children or all of x&#39;s children are in curOrder:
14+
if x is the king return null
15+
else return Successor(x&#39;s parent, curOrder)
16+
else return x&#39;s oldest child who&#39;s not in curOrder
17+
</pre>
18+
19+
<p>For example, assume we have a kingdom that consists of the king, his children Alice and Bob (Alice is older than Bob), and finally Alice&#39;s son Jack.</p>
20+
21+
<ol>
22+
<li>In the beginning, <code>curOrder</code> will be <code>[&quot;king&quot;]</code>.</li>
23+
<li>Calling <code>Successor(king, curOrder)</code> will return Alice, so we append to <code>curOrder</code> to get <code>[&quot;king&quot;, &quot;Alice&quot;]</code>.</li>
24+
<li>Calling <code>Successor(Alice, curOrder)</code> will return Jack, so we append to <code>curOrder</code> to get <code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;]</code>.</li>
25+
<li>Calling <code>Successor(Jack, curOrder)</code> will return Bob, so we append to <code>curOrder</code> to get <code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;, &quot;Bob&quot;]</code>.</li>
26+
<li>Calling <code>Successor(Bob, curOrder)</code> will return <code>null</code>. Thus the order of inheritance will be <code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;, &quot;Bob&quot;]</code>.</li>
27+
</ol>
28+
29+
<p>Using the above function, we can always obtain a unique order of inheritance.</p>
30+
31+
<p>Implement the <code>ThroneInheritance</code> class:</p>
32+
33+
<ul>
34+
<li><code>ThroneInheritance(string kingName)</code> Initializes an object of the <code>ThroneInheritance</code> class. The name of the king is given as part of the constructor.</li>
35+
<li><code>void birth(string parentName, string childName)</code> Indicates that <code>parentName</code> gave birth to <code>childName</code>.</li>
36+
<li><code>void death(string name)</code> Indicates the death of <code>name</code>. The death of the person doesn&#39;t affect the <code>Successor</code> function nor the current inheritance order. You can treat it as just marking the person as dead.</li>
37+
<li><code>string[] getInheritanceOrder()</code> Returns a list representing the current order of inheritance <strong>excluding</strong> dead people.</li>
38+
</ul>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Example 1:</strong></p>
42+
43+
<pre>
44+
<strong>Input</strong>
45+
[&quot;ThroneInheritance&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;birth&quot;, &quot;getInheritanceOrder&quot;, &quot;death&quot;, &quot;getInheritanceOrder&quot;]
46+
[[&quot;king&quot;], [&quot;king&quot;, &quot;andy&quot;], [&quot;king&quot;, &quot;bob&quot;], [&quot;king&quot;, &quot;catherine&quot;], [&quot;andy&quot;, &quot;matthew&quot;], [&quot;bob&quot;, &quot;alex&quot;], [&quot;bob&quot;, &quot;asha&quot;], [null], [&quot;bob&quot;], [null]]
47+
<strong>Output</strong>
48+
[null, null, null, null, null, null, null, [&quot;king&quot;, &quot;andy&quot;, &quot;matthew&quot;, &quot;bob&quot;, &quot;alex&quot;, &quot;asha&quot;, &quot;catherine&quot;], null, [&quot;king&quot;, &quot;andy&quot;, &quot;matthew&quot;, &quot;alex&quot;, &quot;asha&quot;, &quot;catherine&quot;]]
49+
50+
<strong>Explanation</strong>
51+
ThroneInheritance t= new ThroneInheritance(&quot;king&quot;); // order: <strong>king</strong>
52+
t.birth(&quot;king&quot;, &quot;andy&quot;); // order: king &gt; <strong>andy</strong>
53+
t.birth(&quot;king&quot;, &quot;bob&quot;); // order: king &gt; andy &gt; <strong>bob</strong>
54+
t.birth(&quot;king&quot;, &quot;catherine&quot;); // order: king &gt; andy &gt; bob &gt; <strong>catherine</strong>
55+
t.birth(&quot;andy&quot;, &quot;matthew&quot;); // order: king &gt; andy &gt; <strong>matthew</strong> &gt; bob &gt; catherine
56+
t.birth(&quot;bob&quot;, &quot;alex&quot;); // order: king &gt; andy &gt; matthew &gt; bob &gt; <strong>alex</strong> &gt; catherine
57+
t.birth(&quot;bob&quot;, &quot;asha&quot;); // order: king &gt; andy &gt; matthew &gt; bob &gt; alex &gt; <strong>asha</strong> &gt; catherine
58+
t.getInheritanceOrder(); // return [&quot;king&quot;, &quot;andy&quot;, &quot;matthew&quot;, &quot;bob&quot;, &quot;alex&quot;, &quot;asha&quot;, &quot;catherine&quot;]
59+
t.death(&quot;bob&quot;); // order: king &gt; andy &gt; matthew &gt; <strong><s>bob</s></strong> &gt; alex &gt; asha &gt; catherine
60+
t.getInheritanceOrder(); // return [&quot;king&quot;, &quot;andy&quot;, &quot;matthew&quot;, &quot;alex&quot;, &quot;asha&quot;, &quot;catherine&quot;]
61+
</pre>
62+
63+
<p>&nbsp;</p>
64+
<p><strong>Constraints:</strong></p>
65+
66+
<ul>
67+
<li><code>1 &lt;= kingName.length, parentName.length, childName.length, name.length &lt;= 15</code></li>
68+
<li><code>kingName</code>, <code>parentName</code>, <code>childName</code>, and <code>name</code> consist of lowercase English letters only.</li>
69+
<li>All arguments <code>childName</code> and <code>kingName</code> are <strong>distinct</strong>.</li>
70+
<li>All <code>name</code> arguments of <code>death</code> will be passed to either the constructor or as <code>childName</code> to <code>birth</code> first.</li>
71+
<li>For each call to&nbsp;<code>birth(parentName, childName)</code>, it is guaranteed that&nbsp;<code>parentName</code> is alive.</li>
72+
<li>At most <code>10<sup>5</sup></code> calls will be made to <code>birth</code> and <code>death</code>.</li>
73+
<li>At most <code>10</code> calls will be made to <code>getInheritanceOrder</code>.</li>
74+
</ul>
75+
76+
77+
## Solutions
78+
79+
80+
81+
<!-- tabs:start -->
82+
83+
### **Python3**
84+
85+
86+
```python
87+
88+
```
89+
90+
### **Java**
91+
92+
93+
```java
94+
95+
```
96+
97+
### **...**
98+
```
99+
100+
```
101+
102+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [1601. 最多可达成的换楼请求数目](https://leetcode-cn.com/problems/maximum-number-of-achievable-transfer-requests)
2+
3+
[English Version](/solution/1500-1599/1601.Maximum%20Number%20of%20Achievable%20Transfer%20Requests/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
<p>我们有&nbsp;<code>n</code>&nbsp;栋楼,编号从&nbsp;<code>0</code>&nbsp;&nbsp;<code>n - 1</code>&nbsp;。每栋楼有若干员工。由于现在是换楼的季节,部分员工想要换一栋楼居住。</p>
9+
10+
<p>给你一个数组 <code>requests</code>&nbsp;,其中&nbsp;<code>requests[i] = [from<sub>i</sub>, to<sub>i</sub>]</code>&nbsp;,表示一个员工请求从编号为&nbsp;<code>from<sub>i</sub></code>&nbsp;的楼搬到编号为&nbsp;<code>to<sub>i</sub></code><sub>&nbsp;</sub>的楼。</p>
11+
12+
<p>一开始&nbsp;<strong>所有楼都是满的</strong>,所以从请求列表中选出的若干个请求是可行的需要满足 <strong>每栋楼员工净变化为 0&nbsp;</strong>。意思是每栋楼 <strong>离开</strong>&nbsp;的员工数目 <strong>等于</strong>&nbsp;该楼 <strong>搬入</strong>&nbsp;的员工数数目。比方说&nbsp;<code>n = 3</code>&nbsp;且两个员工要离开楼&nbsp;<code>0</code>&nbsp;,一个员工要离开楼&nbsp;<code>1</code>&nbsp;,一个员工要离开楼 <code>2</code>&nbsp;,如果该请求列表可行,应该要有两个员工搬入楼&nbsp;<code>0</code>&nbsp;,一个员工搬入楼&nbsp;<code>1</code>&nbsp;,一个员工搬入楼&nbsp;<code>2</code>&nbsp;。</p>
13+
14+
<p>请你从原请求列表中选出若干个请求,使得它们是一个可行的请求列表,并返回所有可行列表中最大请求数目。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong>示例 1:</strong></p>
19+
20+
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/26/move1.jpg" style="height: 406px; width: 600px;"></p>
21+
22+
<pre><strong>输入:</strong>n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
23+
<strong>输出:</strong>5
24+
<strong>解释:</strong>请求列表如下:
25+
从楼 0 离开的员工为 x 和 y ,且他们都想要搬到楼 1 。
26+
从楼 1 离开的员工为 a 和 b ,且他们分别想要搬到楼 2 和 0 。
27+
从楼 2 离开的员工为 z ,且他想要搬到楼 0 。
28+
从楼 3 离开的员工为 c ,且他想要搬到楼 4 。
29+
没有员工从楼 4 离开。
30+
我们可以让 x 和 b 交换他们的楼,以满足他们的请求。
31+
我们可以让 y,a 和 z 三人在三栋楼间交换位置,满足他们的要求。
32+
所以最多可以满足 5 个请求。</pre>
33+
34+
<p><strong>示例 2:</strong></p>
35+
36+
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/26/move2.jpg" style="height: 327px; width: 450px;"></p>
37+
38+
<pre><strong>输入:</strong>n = 3, requests = [[0,0],[1,2],[2,1]]
39+
<strong>输出:</strong>3
40+
<strong>解释:</strong>请求列表如下:
41+
从楼 0 离开的员工为 x ,且他想要回到原来的楼 0 。
42+
从楼 1 离开的员工为 y ,且他想要搬到楼 2 。
43+
从楼 2 离开的员工为 z ,且他想要搬到楼 1 。
44+
我们可以满足所有的请求。</pre>
45+
46+
<p><strong>示例 3:</strong></p>
47+
48+
<pre><strong>输入:</strong>n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
49+
<strong>输出:</strong>4
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>1 &lt;= n &lt;= 20</code></li>
58+
<li><code>1 &lt;= requests.length &lt;= 16</code></li>
59+
<li><code>requests[i].length == 2</code></li>
60+
<li><code>0 &lt;= from<sub>i</sub>, to<sub>i</sub> &lt; n</code></li>
61+
</ul>
62+
63+
64+
65+
## 解法
66+
67+
<!-- 这里可写通用的实现逻辑 -->
68+
69+
70+
<!-- tabs:start -->
71+
72+
### **Python3**
73+
74+
<!-- 这里可写当前语言的特殊实现逻辑 -->
75+
76+
```python
77+
78+
```
79+
80+
### **Java**
81+
82+
<!-- 这里可写当前语言的特殊实现逻辑 -->
83+
84+
```java
85+
86+
```
87+
88+
### **...**
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->

0 commit comments

Comments
 (0)