Skip to content

feat: update lc problems #3125

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 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions solution/0100-0199/0130.Surrounded Regions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ tags:
<li><strong>围绕:</strong>如果您可以用&nbsp;<code>'X'</code>&nbsp;单元格 <strong>连接这个区域</strong>,并且区域中没有任何单元格位于&nbsp;<code>board</code> 边缘,则该区域被 <code>'X'</code>&nbsp;单元格围绕。</li>
</ul>

<p>通过将输入矩阵&nbsp;<code>board</code> 中的所有 <code>'O'</code>&nbsp;替换为 <code>'X'</code> 来 <strong>捕获被围绕的区域</strong>。</p>

<div class="original__bRMd">
<div>
<p>&nbsp;</p>
Expand Down
37 changes: 34 additions & 3 deletions solution/0400-0499/0431.Encode N-ary Tree to Binary Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,47 @@ tags:

<p>&nbsp;</p>

<p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0431.Encode%20N-ary%20Tree%20to%20Binary%20Tree/images/narytreebinarytreeexample.png" style="width: 500px;"></p>
<p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0431.Encode%20N-ary%20Tree%20to%20Binary%20Tree/images/narytreebinarytreeexample.png" style="width: 500px;" /></p>

<p>&nbsp;</p>

<pre>
<strong>输入:</strong>root = [1,null,3,2,4,null,5,6]
</pre>

<p>注意,上面的方法仅仅是一个例子,可能可行也可能不可行。你没有必要遵循这种形式转化,你可以自己创造和实现不同的方法。</p>

<p><strong>注意:</strong></p>
<p>&nbsp;</p>

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

<pre>
<strong>输入:</strong>root = [1,null,3,2,4,null,5,6]
<strong>输出:</strong>[1,null,3,2,4,null,5,6]
</pre>

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

<pre>
<strong>输入:</strong>root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
<strong>输出:</strong>[1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
</pre>

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

<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>[]
</pre>

<p>&nbsp;</p>

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

<ol>
<li><code>N</code>&nbsp;的范围在 <code>[1, 1000]</code></li>
<li><code>N</code>&nbsp;的范围在 <code>[1, 10<sup>4</sup>]</code></li>
<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
<li>N 叉树的高度小于等于&nbsp;<code>1000</code>。</li>
<li>不要使用类成员 / 全局变量 / 静态变量来存储状态。你的编码和解码算法应是无状态的。</li>
</ol>

Expand Down
1 change: 1 addition & 0 deletions solution/0400-0499/0469.Convex Polygon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0469.Convex%20Polygon/README.md
tags:
- 几何
- 数组
- 数学
---

Expand Down
1 change: 1 addition & 0 deletions solution/0400-0499/0469.Convex Polygon/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0469.Convex%20Polygon/README_EN.md
tags:
- Geometry
- Array
- Math
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0500-0599/0536.Construct%20Binary%20Tree%20from%20String/README.md
tags:
- 栈
- 树
- 深度优先搜索
- 字符串
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0500-0599/0536.Construct%20Binary%20Tree%20from%20String/README_EN.md
tags:
- Stack
- Tree
- Depth-First Search
- String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ tags:
<pre>
class Node {
public boolean val;
    public boolean isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;
&nbsp; &nbsp; public boolean isLeaf;
&nbsp; &nbsp; public Node topLeft;
&nbsp; &nbsp; public Node topRight;
&nbsp; &nbsp; public Node bottomLeft;
&nbsp; &nbsp; public Node bottomRight;
}</pre>

<p>我们可以按以下步骤为二维区域构建四叉树:</p>
Expand All @@ -60,9 +60,9 @@ class Node {

<p>它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 <code>[isLeaf, val]</code> 。</p>

<p>如果 <code>isLeaf</code> 或者 <code>val</code> 的值为 True ,则表示它在列表 <code>[isLeaf, val]</code> 中的值为 <strong>1</strong> ;如果 <code>isLeaf</code> 或者 <code>val</code> 的值为 False ,则表示值为 <strong>0 </strong>。</p>
<p>如果 <code>isLeaf</code> 或者 <code>val</code> 的值为 True ,则表示它在列表&nbsp;<code>[isLeaf, val]</code> 中的值为 <strong>1</strong> ;如果 <code>isLeaf</code> 或者 <code>val</code> 的值为 False ,则表示值为 <strong>0 </strong>。</p>

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

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

Expand All @@ -88,37 +88,13 @@ class Node {
结果矩阵大小为 1*1,值全为 0 。
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>quadTree1 = [[0,0],[1,0],[1,0],[1,1],[1,1]]
, quadTree2 = [[0,0],[1,1],[1,1],[1,0],[1,1]]
<strong>输出:</strong>[[1,1]]
</pre>

<p><strong>示例 4:</strong></p>

<pre>
<strong>输入:</strong>quadTree1 = [[0,0],[1,1],[1,0],[1,1],[1,1]]
, quadTree2 = [[0,0],[1,1],[0,1],[1,1],[1,1],null,null,null,null,[1,1],[1,0],[1,0],[1,1]]
<strong>输出:</strong>[[0,0],[1,1],[0,1],[1,1],[1,1],null,null,null,null,[1,1],[1,0],[1,0],[1,1]]
</pre>

<p><strong>示例 5:</strong></p>

<pre>
<strong>输入:</strong>quadTree1 = [[0,1],[1,0],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
, quadTree2 = [[0,1],[0,1],[1,0],[1,1],[1,0],[1,0],[1,0],[1,1],[1,1]]
<strong>输出:</strong>[[0,0],[0,1],[0,1],[1,1],[1,0],[1,0],[1,0],[1,1],[1,1],[1,0],[1,0],[1,1],[1,1]]
</pre>

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

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

<ul>
<li><code>quadTree1</code> 和 <code>quadTree2</code> 都是符合题目要求的四叉树,每个都代表一个 <code>n * n</code> 的矩阵。</li>
<li><code>n == 2^x</code> ,其中 <code>0 <= x <= 9</code>.</li>
<li><code>n == 2<sup>x</sup></code>&nbsp;,其中 <code>0 &lt;= x &lt;= 9</code>.</li>
</ul>

<!-- description:end -->
Expand Down
2 changes: 1 addition & 1 deletion solution/1400-1499/1490.Clone N-ary Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Node {

<ul>
<li>给定的 N 叉树的深度小于或等于&nbsp;<code>1000</code>。</li>
<li>节点的总个数在&nbsp;<code>[0,&nbsp;10^4]</code>&nbsp;之间</li>
<li>节点的总个数在&nbsp;<code>[0,&nbsp;10<sup>4</sup>]</code>&nbsp;之间</li>
</ul>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/1600-1699/1612.Ch
tags:
- 树
- 深度优先搜索
- 哈希表
- 二叉树
- 计数
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/1600-1699/1612.Ch
tags:
- Tree
- Depth-First Search
- Hash Table
- Binary Tree
- Counting
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/1600-1699/1625.Le
rating: 1992
source: 第 211 场周赛 Q2
tags:
- 深度优先搜索
- 广度优先搜索
- 字符串
- 枚举
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/1600-1699/1625.Le
rating: 1992
source: Weekly Contest 211 Q2
tags:
- Depth-First Search
- Breadth-First Search
- String
- Enumeration
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ tags:
- 栈
- 树
- 设计
- 数组
- 数学
- 二叉树
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ tags:
- Stack
- Tree
- Design
- Array
- Math
- Binary Tree
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tags:
- 哈希表
- 字符串
- 动态规划
- 枚举
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tags:
- Hash Table
- String
- Dynamic Programming
- Enumeration
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ tags:

<p><code>divisors[0]</code>&nbsp;的可整除性分数为 2 因为&nbsp;<code>nums[2]</code> 和&nbsp;<code>nums[3]</code>&nbsp;能被 5 整除。</p>

<p><code>divisors[1]</code> 的可整除性分数为 1 因为只有 <code>nums[1]</code>&nbsp;能被 3 整除。</p>
<p><code>divisors[1]</code> 的可整除性分数为 2 因为&nbsp;<code>nums[1]</code>&nbsp;和&nbsp;<code>nums[2]</code>&nbsp;能被 3 整除。</p>

<p><code>divisors[2]</code> 的可整除性分数为 0 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 7 整除。</p>

<p><code>divisors[3]</code> 的可整除性分数为 2 因为 <code>nums[0]</code> 和&nbsp;<code>nums[3]</code>&nbsp;能够被 2 整除。</p>

<p>因为&nbsp;<code>divisors[0]</code> 和&nbsp;<code>divisors[3]</code>&nbsp;有相同的可整除性分数,我们返回更小的那个&nbsp;<code>divisors[3]</code>。</p>
<p>因为&nbsp;<code>divisors[0]</code>&nbsp;、<code>divisor[1]</code> 和&nbsp;<code>divisors[3]</code>&nbsp;有相同的可整除性分数,我们返回更小的那个&nbsp;<code>divisors[3]</code>。</p>
</div>

<p><strong class="example">示例 2:</strong></p>
Expand Down Expand Up @@ -71,13 +71,13 @@ tags:

<p><strong>解释:</strong></p>

<p><code>divisors[0]</code> 的可整除性分数为 0 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 10 整除。</p>
<p><code>divisors[0]</code> 的可整除性分数为 2 因为&nbsp;<code>nums[0]</code>&nbsp;和&nbsp;<code>nums[3]</code> 能被 10 整除。</p>

<p><code>divisors[1]</code> 的可整除性分数为 0 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 16&nbsp;整除。</p>

<p><code>divisors[2]</code> 的可整除性分数为 0 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 20&nbsp;整除。</p>
<p><code>divisors[2]</code> 的可整除性分数为 1 因为&nbsp;<code>nums[0]</code>&nbsp;能被 20&nbsp;整除。</p>

<p>因为&nbsp;<code>divisors[0]</code>,<code>divisors[1]</code> 和&nbsp;<code>divisors[2]</code>&nbsp;都有相同的可整除性分数,我们返回最小的那个&nbsp;<code>divisors[0]</code>。</p>
<p>因为&nbsp;<code>divisors[0]</code>&nbsp;的可整除性分数最大,我们返回&nbsp;<code>divisors[0]</code>。</p>
</div>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ tags:

<p>The divisibility score of <code>divisors[0]</code> is 2 since <code>nums[2]</code> and <code>nums[3]</code> are divisible by 5.</p>

<p>The divisibility score of <code>divisors[1]</code> is 1 since only <code>nums[1]</code> is divisible by 3.</p>
<p>The divisibility score of <code>divisors[1]</code> is 2 since <code>nums[1]</code> and <code>nums[2]</code> are divisible by 3.</p>

<p>The divisibility score of <code>divisors[2]</code> is 0 since none of the numbers in <code>nums</code> is divisible by 7.</p>

<p>The divisibility score of <code>divisors[3]</code> is 2 since <code>nums[0]</code> and <code>nums[3]</code> are divisible by 2.</p>

<p>As <code>divisors[0]</code> and <code>divisors[3]</code> have the same divisibility score, we return the smaller one which is <code>divisors[3]</code>.</p>
<p>As <code>divisors[0]</code>,&nbsp;<code>divisors[1]</code>, and <code>divisors[3]</code> have the same divisibility score, we return the smaller one which is <code>divisors[3]</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>
Expand Down Expand Up @@ -70,13 +70,11 @@ tags:

<p><strong>Explanation:</strong></p>

<p>The divisibility score of <code>divisors[0]</code> is 0 since none of the numbers in <code>nums</code> is divisible by 10.</p>
<p>The divisibility score of <code>divisors[0]</code> is 2 since <code>nums[0]</code> and <code>nums[3]</code> are divisible by 10.</p>

<p>The divisibility score of <code>divisors[1]</code> is 0 since none of the numbers in <code>nums</code> is divisible by 16.</p>

<p>The divisibility score of <code>divisors[2]</code> is 0 since none of the numbers in <code>nums</code> is divisible by 20.</p>

<p>As <code>divisors[0]</code>, <code>divisors[1]</code> and <code>divisors[2]</code> all have the same divisibility score, we return the smallest one which is <code>divisors[0]</code>.</p>
<p>The divisibility score of <code>divisors[2]</code> is 1 since <code>nums[0]</code> is divisible by 20.</p>
</div>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,17 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:排序 + 动态规划

根据题目描述,我们顺序移动的单元格的值必须严格递增,因此,我们不妨用一个哈希表 $g$ 来记录每个值对应的所有单元格的位置,然后按照值的大小从小到大遍历。

在这个过程中,我们可以维护两个数组 `rowMax` 和 `colMax`,分别记录每一行和每一列的最大递增长度。初始时,这两个数组的所有元素都为 $0$。

对于每个值对应的所有单元格位置,我们按照位置顺序遍历,对于每个位置 $(i, j)$,我们可以计算出以该位置为终点的最大递增长度为 $1 + \max(\text{rowMax}[i], \text{colMax}[j])$,更新答案,然后更新 `rowMax[i]` 和 `colMax[j]`。

最后返回答案即可。

时间复杂度 $O(m \times n \times \log(m \times n))$,空间复杂度 $O(m \times n)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -212,6 +222,51 @@ func maxIncreasingCells(mat [][]int) (ans int) {
}
```

#### TypeScript

```ts
function maxIncreasingCells(mat: number[][]): number {
const m = mat.length;
const n = mat[0].length;
const g: { [key: number]: [number, number][] } = {};

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (!g[mat[i][j]]) {
g[mat[i][j]] = [];
}
g[mat[i][j]].push([i, j]);
}
}

const rowMax = Array(m).fill(0);
const colMax = Array(n).fill(0);
let ans = 0;

const sortedKeys = Object.keys(g)
.map(Number)
.sort((a, b) => a - b);

for (const key of sortedKeys) {
const pos = g[key];
const mx: number[] = [];

for (const [i, j] of pos) {
mx.push(1 + Math.max(rowMax[i], colMax[j]));
ans = Math.max(ans, mx[mx.length - 1]);
}

for (let k = 0; k < pos.length; k++) {
const [i, j] = pos[k];
rowMax[i] = Math.max(rowMax[i], mx[k]);
colMax[j] = Math.max(colMax[j], mx[k]);
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading