Skip to content

Commit 58c1c88

Browse files
yanglbmeidoocs
andauthored
feat: update lc problems (doocs#2889)
--------- Co-authored-by: Doocs Bot <[email protected]>
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 26 additions & 2 deletions
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

Lines changed: 12 additions & 20 deletions
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

Lines changed: 34 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

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

0 commit comments

Comments
 (0)