Skip to content

[pull] main from doocs:main #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ee7ddb9
feat: add solutions to lc problems: No.3151~3153 (#2838)
yanglbme May 19, 2024
3f6a34f
feat: add solutions to lc problem: No.3154 (#2842)
yanglbme May 19, 2024
51b984f
feat: add cpp solution to lc problem: No.3068 (#2839)
AlleyNawaz May 19, 2024
f55b0d7
feat: add cpp solution to lc problem: No.3068 (#2840)
AlleyNawaz May 19, 2024
ac3102d
feat: add cpp solution to lc problem: No.3068 (#2841)
AlleyNawaz May 19, 2024
818daa9
feat: add solutions to lc problem: No.3155 (#2843)
yanglbme May 19, 2024
06533ad
feat: add solutions to lc problem: No.1317 (#2844)
yanglbme May 19, 2024
41ebef4
refactor: update solution with sorting via object to lc problem: No.2…
rain84 May 20, 2024
aa8ed86
refactor: update solution to lc problem: No.2625 (#2848)
rain84 May 20, 2024
dfb0297
feat: add ts solution to lc problem: No.1542 (#2850)
yanglbme May 20, 2024
5e67f9f
feat: update solutions to lc problem: No.2705 (#2851)
yanglbme May 20, 2024
5301c76
feat: update lc problems (#2852)
yanglbme May 20, 2024
4db3cd4
feat: add swift implementation to lcof problem: No.06 (#2853)
klever34 May 20, 2024
d2ee6aa
feat: add swift implementation to lcof problem: No.07 (#2854)
klever34 May 20, 2024
5cc6315
feat: add swift implementation to lcof problem: No.09 (#2855)
klever34 May 20, 2024
d451882
feat: add swift implementation to lcof problem: No.10.1 (#2856)
klever34 May 20, 2024
98beaf4
feat: add swift implementation to lcof problem: No.10.2 (#2857)
klever34 May 20, 2024
5f6a3af
feat: add swift implementation to lcof problem: No.11 (#2858)
klever34 May 20, 2024
df9bd5d
feat: add sql solution to lc problem: No.3156 (#2859)
yanglbme May 20, 2024
14c8085
feat: add solutions to lcp problem: No.80 (#2860)
yanglbme May 20, 2024
feae0f6
feat: update solutions to lc/lcof problems (#2861)
yanglbme May 21, 2024
27036e8
feat: update solutions to lcof problem: No.15 (#2862)
yanglbme May 21, 2024
6bb333d
--- (#2863)
dependabot[bot] May 21, 2024
a9caf56
feat: add swift implementation to lcof problem: No.12 (#2864)
klever34 May 21, 2024
d449a8a
feat: add swift implementation to lcof problem: No.13 (#2865)
klever34 May 21, 2024
7796a59
feat: add swift implementation to lcof problem: No.14.1 (#2866)
klever34 May 21, 2024
fc09c8c
feat: add swift implementation to lcof problem: No.14.2 (#2867)
klever34 May 21, 2024
d6c9b4f
feat: add swift implementation to lcof problem: No.15 (#2868)
klever34 May 21, 2024
c954041
feat: add swift implementation to lcof problem: No.16 (#2869)
klever34 May 21, 2024
3c5c2d6
feat: add solutions to lc problem: No.2831 (#2870)
yanglbme May 21, 2024
3768553
feat: update solutions to lcof problems: No.26,27,30 (#2871)
yanglbme May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: update lc problems (doocs#2852)
  • Loading branch information
yanglbme authored May 20, 2024
commit 5301c760022c1b84870ce22ae4c3b937d5f84cd0
18 changes: 9 additions & 9 deletions solution/0200-0299/0278.First Bad Version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,34 @@ tags:

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

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

<p><strong>示例 1:</strong></p>
<p><strong class="example">示例 1:</strong></p>

<pre>
<strong>输入:</strong>n = 5, bad = 4
<strong>输出:</strong>4
<strong>解释:</strong>
<code>调用 isBadVersion(3) -> false
调用 isBadVersion(5) -> true
调用 isBadVersion(4) -> true</code>
<code>调用 isBadVersion(3) -&gt; false
调用 isBadVersion(5)&nbsp;-&gt; true
调用 isBadVersion(4)&nbsp;-&gt; true</code>
<code>所以,4 是第一个错误的版本。</code>
</pre>

<p><strong>示例 2:</strong></p>
<p><strong class="example">示例 2:</strong></p>

<pre>
<strong>输入:</strong>n = 1, bad = 1
<strong>输出:</strong>1
</pre>

<p> </p>
<p>&nbsp;</p>

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

<ul>
<li><code>1 <= bad <= n <= 2<sup>31</sup> - 1</code></li>
<li><code>1 &lt;= bad &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ tags:

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
<p><strong class="example">示例 1:</strong></p>

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

<p><strong>示例 2:</strong></p>
<p><strong class="example">示例 2:</strong></p>

<pre>
<strong>输入:</strong>matrix = [[-5]], k = 1
Expand Down
14 changes: 7 additions & 7 deletions solution/0600-0699/0685.Redundant Connection II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ tags:

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

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

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

<p> </p>
<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
<p><strong class="example">示例 1:</strong></p>
<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;" />
<pre>
<strong>输入:</strong>edges = [[1,2],[1,3],[2,3]]
<strong>输出:</strong>[2,3]
</pre>

<p><strong>示例 2:</strong></p>
<p><strong class="example">示例 2:</strong></p>
<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;" />
<pre>
<strong>输入:</strong>edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
<strong>输出:</strong>[4,1]
</pre>

<p> </p>
<p>&nbsp;</p>

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

<ul>
<li><code>n == edges.length</code></li>
<li><code>3 <= n <= 1000</code></li>
<li><code>3 &lt;= n &lt;= 1000</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
<li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ tags:

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

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

<ul>
<li>
<p>返回数组第<code>i<sup>th</sup></code>个索引(<strong>0-indexed</strong>)处的值(即<code>secret[i]</code>),或者</p>
<p>返回数组第<code>i<sup>th</sup></code>个索引(<strong>0-indexed</strong>)处的值(即&nbsp;<code>secret[i]</code>),或者</p>
</li>
<li>
<p>如果&nbsp;<code>i</code>&nbsp; 超出了数组的边界,则返回&nbsp;<code>2<sup>31</sup>&nbsp;- 1</code></p>
Expand Down
24 changes: 16 additions & 8 deletions solution/0700-0799/0777.Swap Adjacent in LR String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,38 @@ tags:

<!-- description:start -->

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

<p>&nbsp;</p>

<p><strong>示例 :</strong></p>
<p><strong class="example">示例 1:</strong></p>

<pre><strong>输入:</strong> start = &quot;RXXLRXRXL&quot;, end = &quot;XRLXXRRLX&quot;
<strong>输出:</strong> True
<strong>解释:</strong>
我们可以通过以下几步将start转换成end:
<pre>
<strong>输入:</strong>start = "RXXLRXRXL", end = "XRLXXRRLX"
<strong>输出:</strong>true
<strong>解释:</strong>通过以下步骤我们可以将 start 转化为 end:
RXXLRXRXL -&gt;
XRXLRXRXL -&gt;
XRLXRXRXL -&gt;
XRLXXRRXL -&gt;
XRLXXRRLX
</pre>

<p><strong class="example">示例 2:</strong></p>

<pre>
<strong>输入:</strong>start = "X", end = "L"
<strong>输出:</strong>false
</pre>

<p>&nbsp;</p>

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

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

<!-- description:end -->
Expand Down
2 changes: 1 addition & 1 deletion solution/2500-2599/2502.Design Memory Allocator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ loc.free(7); // 释放 mID 为 7 的所有内存单元。内存数组保持原

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

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

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

Expand Down
22 changes: 20 additions & 2 deletions solution/2500-2599/2502.Design Memory Allocator/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ loc.free(7); // Free all memory units with mID 7. The memory array remains the s

<!-- solution:start -->

### Solution 1
### Solution 1: Brute Force Simulation

The data range of the problem is not large, so we can directly use an array to simulate the memory space.

During initialization, set each element in the array to $0$, indicating it's free.

When the `allocate` method is called, traverse the array, find `size` consecutive free memory units, set them to `mID`, and return the first index.

When the `free` method is called, traverse the array, set all memory units equal to `mID` to $0$, indicating they are free.

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.

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 2
### Solution 2: Hash Table + Ordered Set

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.

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.

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.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,51 +248,4 @@ func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1];

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]

def union(a, b):
pa, pb = find(a), find(b)
if pa == pb:
return
p[pa] = pb
size[pb] += size[pa]

m, n = len(grid), len(grid[0])
arr = sorted((grid[i][j], i, j) for i in range(m) for j in range(n))
k = len(queries)
ans = [0] * k
p = list(range(m * n))
size = [1] * len(p)
j = 0
for i, v in sorted(enumerate(queries), key=lambda x: x[1]):
while j < len(arr) and arr[j][0] < v:
_, a, b = arr[j]
for x, y in pairwise((-1, 0, 1, 0, -1)):
c, d = a + x, b + y
if 0 <= c < m and 0 <= d < n and grid[c][d] < v:
union(a * n + b, c * n + d)
j += 1
if grid[0][0] < v:
ans[i] = size[find(0)]
return ans
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Offline Query + BFS + Priority Queue (Min Heap)

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.

Therefore, we can first sort the `queries` array, and then process each query in ascending order.

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`.

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.

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))$.

<!-- tabs:start -->

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

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]

def union(a, b):
pa, pb = find(a), find(b)
if pa == pb:
return
p[pa] = pb
size[pb] += size[pa]

m, n = len(grid), len(grid[0])
arr = sorted((grid[i][j], i, j) for i in range(m) for j in range(n))
k = len(queries)
ans = [0] * k
p = list(range(m * n))
size = [1] * len(p)
j = 0
for i, v in sorted(enumerate(queries), key=lambda x: x[1]):
while j < len(arr) and arr[j][0] < v:
_, a, b = arr[j]
for x, y in pairwise((-1, 0, 1, 0, -1)):
c, d = a + x, b + y
if 0 <= c < m and 0 <= d < n and grid[c][d] < v:
union(a * n + b, c * n + d)
j += 1
if grid[0][0] < v:
ans[i] = size[find(0)]
return ans
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->

This file was deleted.

2 changes: 1 addition & 1 deletion solution/CONTEST_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ comments: true

## 往期竞赛

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

- [3151. 特殊数组 I](/solution/3100-3199/3151.Special%20Array%20I/README.md)
- [3152. 特殊数组 II](/solution/3100-3199/3152.Special%20Array%20II/README.md)
Expand Down
2 changes: 1 addition & 1 deletion solution/contest.json

Large diffs are not rendered by default.