Skip to content

Commit 58c1c88

Browse files
yanglbmeidoocs
andauthored
feat: update lc problems (doocs#2889)
--------- Co-authored-by: Doocs Bot <doocs-bot@outlook.com>
1 parent dd5b306 commit 58c1c88

File tree

56 files changed

+323
-895
lines changed

Some content is hidden

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

56 files changed

+323
-895
lines changed

lcof2/剑指 Offer II 089. 房屋偷盗/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ $$
7777

7878
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组长度。
7979

80-
注意到当 $i \gt 2$ 时,$f[i]$ 只和 $f[i-1]$ 与 $f[i-2]$ 有关,因此我们可以使用两个变量代替数组,将空间复杂度降到 $O(1)$。
81-
8280
<!-- tabs:start -->
8381

8482
#### Python3
@@ -176,7 +174,9 @@ impl Solution {
176174

177175
<!-- solution:start-->
178176

179-
### 方法二
177+
### 方法二:动态规划(空间优化)
178+
179+
我们注意到,当 $i \gt 2$ 时,$f[i]$ 只和 $f[i-1]$ 与 $f[i-2]$ 有关,因此我们可以使用两个变量代替数组,将空间复杂度降到 $O(1)$。
180180

181181
<!-- tabs:start -->
182182

solution/0100-0199/0165.Compare Version Numbers/README_EN.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ tags:
1717

1818
<!-- description:start -->
1919

20-
<p>Given two <strong>version strings</strong>,&nbsp;<code>version1</code> and <code>version2</code>, compare them. A version string consists of <strong>revisions</strong> separated by dots <code>&#39;.&#39;</code>. The <strong>value of the revision</strong> is its <strong>integer conversion</strong> ignoring leading zeros.</p>
20+
<p>Given two <strong>version strings</strong>, <code>version1</code> and <code>version2</code>, compare them. A version string consists of <strong>revisions</strong> separated by dots <code>&#39;.&#39;</code>. The <strong>value of the revision</strong> is its <strong>integer conversion</strong> ignoring leading zeros.</p>
2121

2222
<p>To compare version strings, compare their revision values in <strong>left-to-right order</strong>. If one of the version strings has fewer revisions, treat the missing revision values as <code>0</code>.</p>
2323

24-
<p><em>Return the following:</em></p>
24+
<p>Return the following:</p>
2525

2626
<ul>
27-
<li>If <code>version1 &lt; version2</code>, return <code>-1</code>.</li>
28-
<li>If <code>version1 &gt; version2</code>, return <code>1</code>.</li>
29-
<li>Otherwise, return <code>0</code>.</li>
27+
<li>If <code>version1 &lt; version2</code>, return -1.</li>
28+
<li>If <code>version1 &gt; version2</code>, return 1.</li>
29+
<li>Otherwise, return 0.</li>
3030
</ul>
3131

3232
<p>&nbsp;</p>

solution/0100-0199/0198.House Robber/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ $$
7979

8080
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组长度。
8181

82-
注意到当 $i \gt 2$ 时,$f[i]$ 只和 $f[i-1]$ 与 $f[i-2]$ 有关,因此我们可以使用两个变量代替数组,将空间复杂度降到 $O(1)$。
83-
8482
<!-- tabs:start -->
8583

8684
#### Python3
@@ -178,7 +176,9 @@ impl Solution {
178176

179177
<!-- solution:start -->
180178

181-
### 方法二
179+
### 方法二:动态规划(空间优化)
180+
181+
我们注意到,当 $i \gt 2$ 时,$f[i]$ 只和 $f[i-1]$ 与 $f[i-2]$ 有关,因此我们可以使用两个变量代替数组,将空间复杂度降到 $O(1)$。
182182

183183
<!-- tabs:start -->
184184

solution/0100-0199/0198.House Robber/README_EN.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,29 @@ Total amount you can rob = 2 + 9 + 1 = 12.
5454

5555
<!-- solution:start -->
5656

57-
### Solution 1
57+
### Solution 1: Dynamic Programming
58+
59+
We define $f[i]$ as the maximum total amount that can be robbed from the first $i$ houses, initially $f[0]=0$, $f[1]=nums[0]$.
60+
61+
Consider the case where $i \gt 1$, the $i$th house has two options:
62+
63+
- Do not rob the $i$th house, the total amount of robbery is $f[i-1]$;
64+
- Rob the $i$th house, the total amount of robbery is $f[i-2]+nums[i-1]$;
65+
66+
Therefore, we can get the state transition equation:
67+
68+
$$
69+
f[i]=
70+
\begin{cases}
71+
0, & i=0 \\
72+
nums[0], & i=1 \\
73+
\max(f[i-1],f[i-2]+nums[i-1]), & i \gt 1
74+
\end{cases}
75+
$$
76+
77+
The final answer is $f[n]$, where $n$ is the length of the array.
78+
79+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
5880

5981
<!-- tabs:start -->
6082

@@ -153,7 +175,9 @@ impl Solution {
153175

154176
<!-- solution:start -->
155177

156-
### Solution 2
178+
### Solution 2: Dynamic Programming (Space Optimization)
179+
180+
We notice that when $i \gt 2$, $f[i]$ is only related to $f[i-1]$ and $f[i-2]$. Therefore, we can use two variables instead of an array to reduce the space complexity to $O(1)$.
157181

158182
<!-- tabs:start -->
159183

solution/0300-0399/0374.Guess Number Higher or Lower/README.md

+12-20
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,23 @@ tags:
1717

1818
<!-- description:start -->
1919

20-
<p>猜数字游戏的规则如下:</p>
20+
<p>我们正在玩猜数字游戏。猜数字游戏的规则如下:</p>
2121

22-
<ul>
23-
<li>每轮游戏,我都会从 <strong>1</strong> 到 <em><strong>n</strong></em> 随机选择一个数字。 请你猜选出的是哪个数字。</li>
24-
<li>如果你猜错了,我会告诉你,你猜测的数字比我选出的数字是大了还是小了。</li>
25-
</ul>
22+
<p>我会从&nbsp;<code>1</code>&nbsp;&nbsp;<code>n</code> 随机选择一个数字。 请你猜选出的是哪个数字。</p>
2623

27-
<p>你可以通过调用一个预先定义好的接口 <code>int guess(int num)</code> 来获取猜测结果,返回值一共有 3 种可能的情况(<code>-1</code>,<code>1</code> 或 <code>0</code>):</p>
24+
<p>如果你猜错了,我会告诉你,我选出的数字比你猜测的数字大了还是小了。</p>
25+
26+
<p>你可以通过调用一个预先定义好的接口 <code>int guess(int num)</code> 来获取猜测结果,返回值一共有三种可能的情况:</p>
2827

2928
<ul>
30-
<li>-1:我选出的数字比你猜的数字小 <code>pick < num</code></li>
31-
<li>1:我选出的数字比你猜的数字大 <code>pick > num</code></li>
32-
<li>0:我选出的数字和你猜的数字一样。恭喜!你猜对了!<code>pick == num</code></li>
29+
<li><code>-1</code>:你猜的数字比我选出的数字大 (即&nbsp;<code>num &gt; pick</code>)。</li>
30+
<li><code>1</code>:你猜的数字比我选出的数字小&nbsp;(即&nbsp;<code>num &lt;&nbsp;pick</code>)。</li>
31+
<li><code>0</code>:你猜的数字与我选出的数字相等。(即&nbsp;<code>num&nbsp;== pick</code>)。</li>
3332
</ul>
3433

3534
<p>返回我选出的数字。</p>
3635

37-
<p> </p>
36+
<p>&nbsp;</p>
3837

3938
<p><strong>示例 1:</strong></p>
4039

@@ -57,20 +56,13 @@ tags:
5756
<strong>输出:</strong>1
5857
</pre>
5958

60-
<p><strong>示例 4:</strong></p>
61-
62-
<pre>
63-
<strong>输入:</strong>n = 2, pick = 2
64-
<strong>输出:</strong>2
65-
</pre>
66-
67-
<p> </p>
59+
<p>&nbsp;</p>
6860

6961
<p><strong>提示:</strong></p>
7062

7163
<ul>
72-
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
73-
<li><code>1 <= pick <= n</code></li>
64+
<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
65+
<li><code>1 &lt;= pick &lt;= n</code></li>
7466
</ul>
7567

7668
<!-- description:end -->

solution/0600-0699/0690.Employee Importance/README.md

+34-9
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,55 @@ tags:
2020

2121
<!-- description:start -->
2222

23-
<p>给定一个保存员工信息的数据结构,它包含了员工 <strong>唯一的 id </strong>,<strong>重要度 </strong>和 <strong>直系下属的 id </strong>。</p>
23+
<p>你有一个保存员工信息的数据结构,它包含了员工唯一的 id ,重要度和直系下属的 id 。</p>
2424

25-
<p>比如,员工 1 是员工 2 的领导,员工 2 是员工 3 的领导。他们相应的重要度为 15 , 10 , 5 。那么员工 1 的数据结构是 [1, 15, [2]] ,员工 2的 数据结构是 [2, 10, [3]] ,员工 3 的数据结构是 [3, 5, []] 。注意虽然员工 3 也是员工 1 的一个下属,但是由于 <strong>并不是直系</strong> 下属,因此没有体现在员工 1 的数据结构中。</p>
25+
<p>给定一个员工数组&nbsp;<code>employees</code>,其中:</p>
2626

27-
<p>现在输入一个公司的所有员工信息,以及单个员工 id ,返回这个员工和他所有下属的重要度之和。</p>
27+
<ul>
28+
<li><code>employees[i].id</code> 是第&nbsp;<code>i</code>&nbsp;个员工的 ID。</li>
29+
<li><code>employees[i].importance</code>&nbsp;是第&nbsp;<code>i</code>&nbsp;个员工的重要度。</li>
30+
<li><code>employees[i].subordinates</code> 是第 <code>i</code> 名员工的直接下属的 ID 列表。</li>
31+
</ul>
32+
33+
<p>给定一个整数&nbsp;<code>id</code>&nbsp;表示一个员工的 ID,返回这个员工和他所有下属的重要度的 <strong>总和</strong>。</p>
2834

29-
<p> </p>
35+
<p>&nbsp;</p>
3036

31-
<p><strong>示例:</strong></p>
37+
<p><strong>示例 1:</strong></p>
38+
39+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0690.Employee%20Importance/images/1716170448-dKZffb-image.png" style="width: 400px; height: 258px;" /></strong></p>
3240

3341
<pre>
34-
<strong>输入:</strong>[[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
42+
<strong>输入:</strong>employees = [[1,5,[2,3]],[2,3,[]],[3,3,[]]], id = 1
3543
<strong>输出:</strong>11
3644
<strong>解释:</strong>
3745
员工 1 自身的重要度是 5 ,他有两个直系下属 2 和 3 ,而且 2 和 3 的重要度均为 3 。因此员工 1 的总重要度是 5 + 3 + 3 = 11 。
3846
</pre>
3947

40-
<p> </p>
48+
<p>&nbsp;</p>
49+
50+
<p><strong>示例 2:</strong></p>
51+
52+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0690.Employee%20Importance/images/1716170929-dkWpra-image.png" style="width: 362px; height: 361px;" /></strong></p>
53+
54+
<pre>
55+
<strong>输入:</strong>employees = [[1,2,[5]],[5,-3,[]]], id = 5
56+
<strong>输出:</strong>-3
57+
<strong>解释:</strong>员工 5 的重要度为 -3 并且没有直接下属。
58+
因此,员工 5 的总重要度为 -3。
59+
</pre>
60+
61+
<p>&nbsp;</p>
4162

4263
<p><strong>提示:</strong></p>
4364

4465
<ul>
45-
<li>一个员工最多有一个<strong> 直系 </strong>领导,但是可以有多个 <strong>直系 </strong>下属</li>
46-
<li>员工数量不超过 2000 。</li>
66+
<li><code>1 &lt;= employees.length &lt;= 2000</code></li>
67+
<li><code>1 &lt;= employees[i].id &lt;= 2000</code></li>
68+
<li>所有的&nbsp;<code>employees[i].id</code>&nbsp;<strong>互不相同</strong>。</li>
69+
<li><code>-100 &lt;= employees[i].importance &lt;= 100</code></li>
70+
<li>一名员工最多有一名直接领导,并可能有多名下属。</li>
71+
<li><code>employees[i].subordinates</code>&nbsp;中的 ID 都有效。</li>
4772
</ul>
4873

4974
<!-- description:end -->
Loading
Loading

solution/0700-0799/0732.My Calendar III/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tags:
2020

2121
<!-- description:start -->
2222

23-
<p>当 <code>k</code> 个日程安排有一些时间上的交叉时(例如 <code>k</code> 个日程安排都在同一时间内),就会产生 <code>k</code> 次预订。</p>
23+
<p>当 <code>k</code> 个日程存在一些非空交集时(即, <code>k</code> 个日程包含了一些相同时间),就会产生 <code>k</code> 次预订。</p>
2424

2525
<p>给你一些日程安排 <code>[startTime, endTime)</code> ,请你在每个日程安排添加后,返回一个整数 <code>k</code> ,表示所有先前日程安排会产生的最大 <code>k</code> 次预订。</p>
2626

solution/0900-0999/0988.Smallest String Starting From Leaf/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ tags:
66
-
77
- 深度优先搜索
88
- 字符串
9+
- 回溯
910
- 二叉树
1011
---
1112

solution/0900-0999/0988.Smallest String Starting From Leaf/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ tags:
66
- Tree
77
- Depth-First Search
88
- String
9+
- Backtracking
910
- Binary Tree
1011
---
1112

solution/2500-2599/2532.Time to Cross a Bridge/README_EN.md

+45-38
Original file line numberDiff line numberDiff line change
@@ -20,68 +20,75 @@ tags:
2020

2121
<!-- description:start -->
2222

23-
<p>There are <code>k</code> workers who want to move <code>n</code> boxes from an old warehouse to a new one. You are given the two integers <code>n</code> and <code>k</code>, and a 2D integer array <code>time</code> of size <code>k x 4</code> where <code>time[i] = [leftToRight<sub>i</sub>, pickOld<sub>i</sub>, rightToLeft<sub>i</sub>, putNew<sub>i</sub>]</code>.</p>
23+
<p>There are <code>k</code> workers who want to move <code>n</code> boxes from the right (old) warehouse to the left (new) warehouse. You are given the two integers <code>n</code> and <code>k</code>, and a 2D integer array <code>time</code> of size <code>k x 4</code> where <code>time[i] = [right<sub>i</sub>, pick<sub>i</sub>, left<sub>i</sub>, put<sub>i</sub>]</code>.</p>
2424

25-
<p>The warehouses are separated by a river and connected by a bridge. The old warehouse is on the right bank of the river, and the new warehouse is on the left bank of the river. Initially, all <code>k</code> workers are waiting on the left side of the bridge. To move the boxes, the <code>i<sup>th</sup></code> worker (<strong>0-indexed</strong>) can :</p>
25+
<p>The warehouses are separated by a river and connected by a bridge. Initially, all <code>k</code> workers are waiting on the left side of the bridge. To move the boxes, the <code>i<sup>th</sup></code> worker can do the following:</p>
2626

2727
<ul>
28-
<li>Cross the bridge from the left bank (new warehouse) to the right bank (old warehouse) in <code>leftToRight<sub>i</sub></code> minutes.</li>
29-
<li>Pick a box from the old warehouse and return to the bridge in <code>pickOld<sub>i</sub></code> minutes. Different workers can pick up their boxes simultaneously.</li>
30-
<li>Cross the bridge from the right bank (old warehouse) to the left bank (new warehouse) in <code>rightToLeft<sub>i</sub></code> minutes.</li>
31-
<li>Put the box in the new warehouse and return to the bridge in <code>putNew<sub>i</sub></code> minutes. Different workers can put their boxes simultaneously.</li>
28+
<li>Cross the bridge to the right side in <code>right<sub>i</sub></code> minutes.</li>
29+
<li>Pick a box from the right warehouse in <code>pick<sub>i</sub></code> minutes.</li>
30+
<li>Cross the bridge to the left side in <code>left<sub>i</sub></code> minutes.</li>
31+
<li>Put the box into the left warehouse in <code>put<sub>i</sub></code> minutes.</li>
3232
</ul>
3333

34-
<p>A worker <code>i</code> is <strong>less efficient</strong> than a worker <code>j</code> if either condition is met:</p>
34+
<p>The <code>i<sup>th</sup></code> worker is <strong>less efficient</strong> than the j<code><sup>th</sup></code> worker if either condition is met:</p>
3535

3636
<ul>
37-
<li><code>leftToRight<sub>i</sub> + rightToLeft<sub>i</sub> &gt; leftToRight<sub>j</sub> + rightToLeft<sub>j</sub></code></li>
38-
<li><code>leftToRight<sub>i</sub> + rightToLeft<sub>i</sub> == leftToRight<sub>j</sub> + rightToLeft<sub>j</sub></code> and <code>i &gt; j</code></li>
37+
<li><code>left<sub>i</sub> + right<sub>i</sub> &gt; left<sub>j</sub> + right<sub>j</sub></code></li>
38+
<li><code>left<sub>i</sub> + right<sub>i</sub> == left<sub>j</sub> + right<sub>j</sub></code> and <code>i &gt; j</code></li>
3939
</ul>
4040

41-
<p>The following rules regulate the movement of the workers through the bridge :</p>
41+
<p>The following rules regulate the movement of the workers through the bridge:</p>
4242

4343
<ul>
44-
<li>If a worker <code>x</code> reaches the bridge while another worker <code>y</code> is crossing the bridge, <code>x</code> waits at their side of the bridge.</li>
45-
<li>If the bridge is free, the worker waiting on the right side of the bridge gets to cross the bridge. If more than one worker is waiting on the right side, the one with <strong>the lowest efficiency</strong> crosses first.</li>
46-
<li>If the bridge is free and no worker is waiting on the right side, and at least one box remains at the old warehouse, the worker on the left side of the river gets to cross the bridge. If more than one worker is waiting on the left side, the one with <strong>the lowest efficiency</strong> crosses first.</li>
44+
<li>Only one worker can use the bridge at a time.</li>
45+
<li>When the bridge is unused prioritize the <strong>least efficient</strong> worker on the right side to cross. If there are no workers on the right side, prioritize the <strong>least efficient</strong> worker on the left side to cross.</li>
4746
</ul>
4847

49-
<p>Return <em>the instance of time at which the last worker <strong>reaches the left bank</strong> of the river after all n boxes have been put in the new warehouse</em>.</p>
48+
<p>Return the <strong>elapsed minutes</strong> at which the last box reaches the <strong>left side of the bridge</strong>.</p>
5049

5150
<p>&nbsp;</p>
5251
<p><strong class="example">Example 1:</strong></p>
5352

53+
<div class="example-block">
54+
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]</span></p>
55+
56+
<p><strong>Output:</strong> <span class="example-io">6</span></p>
57+
58+
<p><strong>Explanation:</strong></p>
59+
5460
<pre>
55-
<strong>Input:</strong> n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]
56-
<strong>Output:</strong> 6
57-
<strong>Explanation: </strong>
58-
From 0 to 1: worker 2 crosses the bridge from the left bank to the right bank.
59-
From 1 to 2: worker 2 picks up a box from the old warehouse.
60-
From 2 to 6: worker 2 crosses the bridge from the right bank to the left bank.
61-
From 6 to 7: worker 2 puts a box at the new warehouse.
62-
The whole process ends after 7 minutes. We return 6 because the problem asks for the instance of time at which the last worker reaches the left bank.
61+
From 0 to 1 minutes: worker 2 crosses the bridge to the right.
62+
From 1 to 2 minutes: worker 2 picks up a box from the right warehouse.
63+
From 2 to 6 minutes: worker 2 crosses the bridge to the left.
64+
From 6 to 7 minutes: worker 2 puts a box at the left warehouse.
65+
The whole process ends after 7 minutes. We return 6 because the problem asks for the instance of time at which the last worker reaches the left side of the bridge.
6366
</pre>
67+
</div>
6468

6569
<p><strong class="example">Example 2:</strong></p>
6670

71+
<div class="example-block">
72+
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 2, time = [[1,9,1,8],[10,10,10,10]]</span></p>
73+
74+
<p><strong>Output:</strong> <span class="example-io">50</span></p>
75+
76+
<p><strong>Explanation:</strong></p>
77+
6778
<pre>
68-
<strong>Input:</strong> n = 3, k = 2, time = [[1,9,1,8],[10,10,10,10]]
69-
<strong>Output:</strong> 50
70-
<strong>Explanation:</strong>
71-
From 0 &nbsp;to 10: worker 1 crosses the bridge from the left bank to the right bank.
72-
From 10 to 20: worker 1 picks up a box from the old warehouse.
73-
From 10 to 11: worker 0 crosses the bridge from the left bank to the right bank.
74-
From 11 to 20: worker 0 picks up a box from the old warehouse.
75-
From 20 to 30: worker 1 crosses the bridge from the right bank to the left bank.
76-
From 30 to 40: worker 1 puts a box at the new warehouse.
77-
From 30 to 31: worker 0 crosses the bridge from the right bank to the left bank.
78-
From 31 to 39: worker 0 puts a box at the new warehouse.
79-
From 39 to 40: worker 0 crosses the bridge from the left bank to the right bank.
80-
From 40 to 49: worker 0 picks up a box from the old warehouse.
81-
From 49 to 50: worker 0 crosses the bridge from the right bank to the left bank.
82-
From 50 to 58: worker 0 puts a box at the new warehouse.
83-
The whole process ends after 58 minutes. We return 50 because the problem asks for the instance of time at which the last worker reaches the left bank.
79+
From 0 to 10: worker 1 crosses the bridge to the right.
80+
From 10 to 20: worker 1 picks up a box from the right warehouse.
81+
From 10 to 11: worker 0 crosses the bridge to the right.
82+
From 11 to 20: worker 0 picks up a box from the right warehouse.
83+
From 20 to 30: worker 1 crosses the bridge to the left.
84+
From 30 to 40: worker 1 puts a box at the left warehouse.
85+
From 30 to 31: worker 0 crosses the bridge to the left.
86+
From 31 to 39: worker 0 puts a box at the left warehouse.
87+
From 39 to 40: worker 0 crosses the bridge to the right.
88+
From 40 to 49: worker 0 picks up a box from the right warehouse.
89+
From 49 to 50: worker 0 crosses the bridge to the left.
8490
</pre>
91+
</div>
8592

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

0 commit comments

Comments
 (0)