Skip to content

Commit 5301c76

Browse files
authored
feat: update lc problems (doocs#2852)
1 parent 5e67f9f commit 5301c76

File tree

12 files changed

+70
-160
lines changed

12 files changed

+70
-160
lines changed

solution/0200-0299/0278.First Bad Version/README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,34 @@ tags:
2121

2222
<p>假设你有 <code>n</code> 个版本 <code>[1, 2, ..., n]</code>,你想找出导致之后所有版本出错的第一个错误的版本。</p>
2323

24-
<p>你可以通过调用 <code>bool isBadVersion(version)</code> 接口来判断版本号 <code>version</code> 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。</p>
25-
 
24+
<p>你可以通过调用&nbsp;<code>bool isBadVersion(version)</code>&nbsp;接口来判断版本号 <code>version</code> 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。</p>
25+
&nbsp;
2626

27-
<p><strong>示例 1:</strong></p>
27+
<p><strong class="example">示例 1:</strong></p>
2828

2929
<pre>
3030
<strong>输入:</strong>n = 5, bad = 4
3131
<strong>输出:</strong>4
3232
<strong>解释:</strong>
33-
<code>调用 isBadVersion(3) -> false
34-
调用 isBadVersion(5) -> true
35-
调用 isBadVersion(4) -> true</code>
33+
<code>调用 isBadVersion(3) -&gt; false
34+
调用 isBadVersion(5)&nbsp;-&gt; true
35+
调用 isBadVersion(4)&nbsp;-&gt; true</code>
3636
<code>所以,4 是第一个错误的版本。</code>
3737
</pre>
3838

39-
<p><strong>示例 2:</strong></p>
39+
<p><strong class="example">示例 2:</strong></p>
4040

4141
<pre>
4242
<strong>输入:</strong>n = 1, bad = 1
4343
<strong>输出:</strong>1
4444
</pre>
4545

46-
<p> </p>
46+
<p>&nbsp;</p>
4747

4848
<p><strong>提示:</strong></p>
4949

5050
<ul>
51-
<li><code>1 <= bad <= n <= 2<sup>31</sup> - 1</code></li>
51+
<li><code>1 &lt;= bad &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
5252
</ul>
5353

5454
<!-- description:end -->

solution/0300-0399/0378.Kth Smallest Element in a Sorted Matrix/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ tags:
2727

2828
<p>&nbsp;</p>
2929

30-
<p><strong>示例 1:</strong></p>
30+
<p><strong class="example">示例 1:</strong></p>
3131

3232
<pre>
3333
<strong>输入:</strong>matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
3434
<strong>输出:</strong>13
3535
<strong>解释:</strong>矩阵中的元素为 [1,5,9,10,11,12,13,<strong>13</strong>,15],第 8 小元素是 13
3636
</pre>
3737

38-
<p><strong>示例 2:</strong></p>
38+
<p><strong class="example">示例 2:</strong></p>
3939

4040
<pre>
4141
<strong>输入:</strong>matrix = [[-5]], k = 1

solution/0600-0699/0685.Redundant Connection II/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,35 @@ tags:
2323

2424
<p>输入一个有向图,该图由一个有着 <code>n</code> 个节点(节点值不重复,从 <code>1</code> 到 <code>n</code>)的树及一条附加的有向边构成。附加的边包含在 <code>1</code> 到 <code>n</code> 中的两个不同顶点间,这条附加的边不属于树中已存在的边。</p>
2525

26-
<p>结果图是一个以边组成的二维数组 <code>edges</code> 。 每个元素是一对 <code>[u<sub>i</sub>, v<sub>i</sub>]</code>,用以表示 <strong>有向 </strong>图中连接顶点 <code>u<sub>i</sub></code> 和顶点 <code>v<sub>i</sub></code> 的边,其中 <code>u<sub>i</sub></code> 是 <code>v<sub>i</sub></code> 的一个父节点。</p>
26+
<p>结果图是一个以边组成的二维数组&nbsp;<code>edges</code> 。 每个元素是一对 <code>[u<sub>i</sub>, v<sub>i</sub>]</code>,用以表示 <strong>有向 </strong>图中连接顶点 <code>u<sub>i</sub></code> 和顶点 <code>v<sub>i</sub></code> 的边,其中 <code>u<sub>i</sub></code> 是 <code>v<sub>i</sub></code> 的一个父节点。</p>
2727

2828
<p>返回一条能删除的边,使得剩下的图是有 <code>n</code> 个节点的有根树。若有多个答案,返回最后出现在给定二维数组的答案。</p>
2929

30-
<p> </p>
30+
<p>&nbsp;</p>
3131

32-
<p><strong>示例 1:</strong></p>
32+
<p><strong class="example">示例 1:</strong></p>
3333
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0685.Redundant%20Connection%20II/images/graph1.jpg" style="width: 222px; height: 222px;" />
3434
<pre>
3535
<strong>输入:</strong>edges = [[1,2],[1,3],[2,3]]
3636
<strong>输出:</strong>[2,3]
3737
</pre>
3838

39-
<p><strong>示例 2:</strong></p>
39+
<p><strong class="example">示例 2:</strong></p>
4040
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0685.Redundant%20Connection%20II/images/graph2.jpg" style="width: 222px; height: 382px;" />
4141
<pre>
4242
<strong>输入:</strong>edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
4343
<strong>输出:</strong>[4,1]
4444
</pre>
4545

46-
<p> </p>
46+
<p>&nbsp;</p>
4747

4848
<p><strong>提示:</strong></p>
4949

5050
<ul>
5151
<li><code>n == edges.length</code></li>
52-
<li><code>3 <= n <= 1000</code></li>
52+
<li><code>3 &lt;= n &lt;= 1000</code></li>
5353
<li><code>edges[i].length == 2</code></li>
54-
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
54+
<li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li>
5555
</ul>
5656

5757
<!-- description:end -->

solution/0700-0799/0702.Search in a Sorted Array of Unknown Size/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ tags:
2020

2121
<p>这是一个<strong>交互问题</strong>。</p>
2222

23-
<p>您有一个<strong>升序</strong>整数数组,其<strong>长度未知</strong>。您没有访问数组的权限,但是可以使用&nbsp;<code>ArrayReader&nbsp;</code>接口访问它。你可以调用&nbsp;<code>ArrayReader.get(i)</code>:</p>
23+
<p>您有一个<strong>升序</strong>整数数组,其<strong>长度未知</strong>。您没有访问数组的权限,但是可以使用&nbsp;<code>ArrayReader</code>&nbsp;接口访问它。你可以调用&nbsp;<code>ArrayReader.get(i)</code>:</p>
2424

2525
<ul>
2626
<li>
27-
<p>返回数组第<code>i<sup>th</sup></code>个索引(<strong>0-indexed</strong>)处的值(即<code>secret[i]</code>),或者</p>
27+
<p>返回数组第<code>i<sup>th</sup></code>个索引(<strong>0-indexed</strong>)处的值(即&nbsp;<code>secret[i]</code>),或者</p>
2828
</li>
2929
<li>
3030
<p>如果&nbsp;<code>i</code>&nbsp; 超出了数组的边界,则返回&nbsp;<code>2<sup>31</sup>&nbsp;- 1</code></p>

solution/0700-0799/0777.Swap Adjacent in LR String/README.md

+16-8
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,38 @@ tags:
1717

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

20-
<p>在一个由 <code>&#39;L&#39;</code> , <code>&#39;R&#39;</code> 和 <code>&#39;X&#39;</code> 三个字符组成的字符串(例如<code>&quot;RXXLRXRXL&quot;</code>)中进行移动操作。一次移动操作指用一个<code>&quot;LX&quot;</code>替换一个<code>&quot;XL&quot;</code>,或者用一个<code>&quot;XR&quot;</code>替换一个<code>&quot;RX&quot;</code>。现给定起始字符串<code>start</code>和结束字符串<code>end</code>,请编写代码,当且仅当存在一系列移动操作使得<code>start</code>可以转换成<code>end</code>时, 返回<code>True</code>。</p>
20+
<p>在一个由 <code>'L'</code> , <code>'R'</code> 和 <code>'X'</code> 三个字符组成的字符串(例如<code>"RXXLRXRXL"</code>)中进行移动操作。一次移动操作指用一个&nbsp;<code>"LX"</code>&nbsp;替换一个&nbsp;<code>"XL"</code>,或者用一个&nbsp;<code>"XR"</code>&nbsp;替换一个&nbsp;<code>"RX"</code>。现给定起始字符串&nbsp;<code>start</code>&nbsp;和结束字符串&nbsp;<code>end</code>,请编写代码,当且仅当存在一系列移动操作使得&nbsp;<code>start</code>&nbsp;可以转换成&nbsp;<code>end</code>&nbsp;时, 返回&nbsp;<code>True</code>。</p>
2121

2222
<p>&nbsp;</p>
2323

24-
<p><strong>示例 :</strong></p>
24+
<p><strong class="example">示例 1:</strong></p>
2525

26-
<pre><strong>输入:</strong> start = &quot;RXXLRXRXL&quot;, end = &quot;XRLXXRRLX&quot;
27-
<strong>输出:</strong> True
28-
<strong>解释:</strong>
29-
我们可以通过以下几步将start转换成end:
26+
<pre>
27+
<strong>输入:</strong>start = "RXXLRXRXL", end = "XRLXXRRLX"
28+
<strong>输出:</strong>true
29+
<strong>解释:</strong>通过以下步骤我们可以将 start 转化为 end:
3030
RXXLRXRXL -&gt;
3131
XRXLRXRXL -&gt;
3232
XRLXRXRXL -&gt;
3333
XRLXXRRXL -&gt;
3434
XRLXXRRLX
3535
</pre>
3636

37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<pre>
40+
<strong>输入:</strong>start = "X", end = "L"
41+
<strong>输出:</strong>false
42+
</pre>
43+
3744
<p>&nbsp;</p>
3845

3946
<p><strong>提示:</strong></p>
4047

4148
<ul>
42-
<li><code>1 &lt;= len(start) = len(end) &lt;= 10000</code>。</li>
43-
<li><code>start</code>和<code>end</code>中的字符串仅限于<code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>和<code>&#39;X&#39;</code>。</li>
49+
<li><code>1 &lt;= start.length&nbsp;&lt;= 10<sup>4</sup></code></li>
50+
<li><code>start.length == end.length</code></li>
51+
<li><code>start</code> 和&nbsp;<code>end</code>&nbsp;都只包含&nbsp;<code>'L'</code>, <code>'R'</code>&nbsp;或&nbsp;<code>'X'</code>。</li>
4452
</ul>
4553

4654
<!-- description:end -->

solution/2500-2599/2502.Design Memory Allocator/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ loc.free(7); // 释放 mID 为 7 的所有内存单元。内存数组保持原
8888

8989
题目数据范围不大,可以直接用数组模拟内存空间。
9090

91-
初始化时,将数组中的每个元素置为 0,表示空闲。
91+
初始化时,将数组中的每个元素置为 $0$,表示空闲。
9292

9393
当调用 `allocate` 方法时,遍历数组,找到连续的 `size` 个空闲内存单元,将其置为 `mID`,并返回第一个下标。
9494

solution/2500-2599/2502.Design Memory Allocator/README_EN.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,17 @@ loc.free(7); // Free all memory units with mID 7. The memory array remains the s
8383

8484
<!-- solution:start -->
8585

86-
### Solution 1
86+
### Solution 1: Brute Force Simulation
87+
88+
The data range of the problem is not large, so we can directly use an array to simulate the memory space.
89+
90+
During initialization, set each element in the array to $0$, indicating it's free.
91+
92+
When the `allocate` method is called, traverse the array, find `size` consecutive free memory units, set them to `mID`, and return the first index.
93+
94+
When the `free` method is called, traverse the array, set all memory units equal to `mID` to $0$, indicating they are free.
95+
96+
The time complexity is $O(n \times q)$, and the space complexity is $O(n)$, where $n$ and $q$ are the size of the memory space and the number of method calls, respectively.
8797

8898
<!-- tabs:start -->
8999

@@ -268,7 +278,15 @@ func (this *Allocator) Free(mID int) (ans int) {
268278

269279
<!-- solution:start -->
270280

271-
### Solution 2
281+
### Solution 2: Hash Table + Ordered Set
282+
283+
We can use an ordered set to maintain the start and end indices of all allocated memory units, where the start index is the key and the end index is the value. Additionally, we use a hash table to maintain the `mID` and its corresponding start index of the memory unit.
284+
285+
When the `allocate` method is called, we traverse the ordered set, find the first free interval with a length greater than or equal to `size`, allocate it to `mID`, and update the ordered set. Then we add the `mID` and its corresponding start index of the memory unit to the hash table.
286+
287+
When the `free` method is called, we find the start index of the memory unit corresponding to `mID` from the hash table, then delete it from the ordered set, and then delete `mID` from the hash table.
288+
289+
The time complexity is $O(q \log n)$, and the space complexity is $O(n)$, where $n$ and $q$ are the size of the memory space and the number of method calls, respectively.
272290

273291
<!-- tabs:start -->
274292

solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README.md

-47
Original file line numberDiff line numberDiff line change
@@ -248,51 +248,4 @@ func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1];
248248

249249
<!-- solution:end -->
250250

251-
<!-- solution:start -->
252-
253-
### 方法二
254-
255-
<!-- tabs:start -->
256-
257-
#### Python3
258-
259-
```python
260-
class Solution:
261-
def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]:
262-
def find(x):
263-
if p[x] != x:
264-
p[x] = find(p[x])
265-
return p[x]
266-
267-
def union(a, b):
268-
pa, pb = find(a), find(b)
269-
if pa == pb:
270-
return
271-
p[pa] = pb
272-
size[pb] += size[pa]
273-
274-
m, n = len(grid), len(grid[0])
275-
arr = sorted((grid[i][j], i, j) for i in range(m) for j in range(n))
276-
k = len(queries)
277-
ans = [0] * k
278-
p = list(range(m * n))
279-
size = [1] * len(p)
280-
j = 0
281-
for i, v in sorted(enumerate(queries), key=lambda x: x[1]):
282-
while j < len(arr) and arr[j][0] < v:
283-
_, a, b = arr[j]
284-
for x, y in pairwise((-1, 0, 1, 0, -1)):
285-
c, d = a + x, b + y
286-
if 0 <= c < m and 0 <= d < n and grid[c][d] < v:
287-
union(a * n + b, c * n + d)
288-
j += 1
289-
if grid[0][0] < v:
290-
ans[i] = size[find(0)]
291-
return ans
292-
```
293-
294-
<!-- tabs:end -->
295-
296-
<!-- solution:end -->
297-
298251
<!-- problem:end -->

solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README_EN.md

+11-48
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,17 @@ tags:
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Offline Query + BFS + Priority Queue (Min Heap)
76+
77+
According to the problem description, each query is independent, the order of the queries does not affect the result, and we are required to start from the top left corner each time, counting the number of cells that can be accessed and whose value is less than the current query value.
78+
79+
Therefore, we can first sort the `queries` array, and then process each query in ascending order.
80+
81+
We use a priority queue (min heap) to maintain the smallest cell value that we have currently accessed, and use an array or hash table `vis` to record whether the current cell has been visited. Initially, we add the data $(grid[0][0], 0, 0)$ of the top left cell as a tuple to the priority queue, and set `vis[0][0]` to `True`.
82+
83+
For each query `queries[i]`, we judge whether the minimum value of the current priority queue is less than `queries[i]`. If it is, we pop the current minimum value, increment the counter `cnt`, and add the four cells above, below, left, and right of the current cell to the priority queue, noting to check whether they have been visited. Repeat the above operation until the minimum value of the current priority queue is greater than or equal to `queries[i]`, at which point `cnt` is the answer to the current query.
84+
85+
The time complexity is $O(k \times \log k + m \times n \log(m \times n))$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the grid, and $k$ is the number of queries. We need to sort the `queries` array, which has a time complexity of $O(k \times \log k)$. Each cell in the matrix will be visited at most once, and the time complexity of each enqueue and dequeue operation is $O(\log(m \times n))$. Therefore, the total time complexity is $O(k \times \log k + m \times n \log(m \times n))$.
7686

7787
<!-- tabs:start -->
7888

@@ -236,51 +246,4 @@ func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1];
236246

237247
<!-- solution:end -->
238248

239-
<!-- solution:start -->
240-
241-
### Solution 2
242-
243-
<!-- tabs:start -->
244-
245-
#### Python3
246-
247-
```python
248-
class Solution:
249-
def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]:
250-
def find(x):
251-
if p[x] != x:
252-
p[x] = find(p[x])
253-
return p[x]
254-
255-
def union(a, b):
256-
pa, pb = find(a), find(b)
257-
if pa == pb:
258-
return
259-
p[pa] = pb
260-
size[pb] += size[pa]
261-
262-
m, n = len(grid), len(grid[0])
263-
arr = sorted((grid[i][j], i, j) for i in range(m) for j in range(n))
264-
k = len(queries)
265-
ans = [0] * k
266-
p = list(range(m * n))
267-
size = [1] * len(p)
268-
j = 0
269-
for i, v in sorted(enumerate(queries), key=lambda x: x[1]):
270-
while j < len(arr) and arr[j][0] < v:
271-
_, a, b = arr[j]
272-
for x, y in pairwise((-1, 0, 1, 0, -1)):
273-
c, d = a + x, b + y
274-
if 0 <= c < m and 0 <= d < n and grid[c][d] < v:
275-
union(a * n + b, c * n + d)
276-
j += 1
277-
if grid[0][0] < v:
278-
ans[i] = size[find(0)]
279-
return ans
280-
```
281-
282-
<!-- tabs:end -->
283-
284-
<!-- solution:end -->
285-
286249
<!-- problem:end -->

solution/2500-2599/2503.Maximum Number of Points From Grid Queries/Solution2.py

-32
This file was deleted.

solution/CONTEST_README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ comments: true
2626

2727
## 往期竞赛
2828

29-
#### 第 398 场周赛(2024-05-19 10:30, 90 分钟) 参赛人数 3605
29+
#### 第 398 场周赛(2024-05-19 10:30, 90 分钟) 参赛人数 3606
3030

3131
- [3151. 特殊数组 I](/solution/3100-3199/3151.Special%20Array%20I/README.md)
3232
- [3152. 特殊数组 II](/solution/3100-3199/3152.Special%20Array%20II/README.md)

solution/contest.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)