Skip to content
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

feat: update lc problems #2459

Merged
merged 1 commit into from
Mar 18, 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
44 changes: 14 additions & 30 deletions solution/0000-0099/0057.Insert Interval/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

<!-- 这里写题目描述 -->

<p>给你一个<strong> 无重叠的</strong><em> ,</em>按照区间起始端点排序的区间列表。</p>
<p>给你一个<strong> 无重叠的</strong><em> ,</em>按照区间起始端点排序的区间列表 <code>intervals</code>,其中&nbsp;<code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个区间的开始和结束,并且&nbsp;<code>intervals</code>&nbsp;按照&nbsp;<code>start<sub>i</sub></code>&nbsp;升序排列。同样给定一个区间&nbsp;<code>newInterval = [start, end]</code>&nbsp;表示另一个区间的开始和结束。</p>

<p>在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。</p>
<p>在&nbsp;<code>intervals</code> 中插入区间&nbsp;<code>newInterval</code>,使得&nbsp;<code>intervals</code>&nbsp;依然按照&nbsp;<code>start<sub>i</sub></code>&nbsp;升序排列,且区间之间不重叠(如果有必要的话,可以合并区间)。</p>

<p> </p>
<p>返回插入之后的&nbsp;<code>intervals</code>。</p>

<p><strong>示例 1:</strong></p>
<p><strong>注意</strong> 你不需要原地修改&nbsp;<code>intervals</code>。你可以创建一个新数组然后返回它。</p>

<p>&nbsp;</p>

<p><strong>示例&nbsp;1:</strong></p>

<pre>
<strong>输入:</strong>intervals = [[1,3],[6,9]], newInterval = [2,5]
Expand All @@ -26,40 +30,20 @@
<pre>
<strong>输入:</strong>intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
<strong>输出:</strong>[[1,2],[3,10],[12,16]]
<strong>解释:</strong>这是因为新的区间 <code>[4,8]</code> 与 <code>[3,5],[6,7],[8,10]</code> 重叠。</pre>

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

<pre>
<strong>输入:</strong>intervals = [], newInterval = [5,7]
<strong>输出:</strong>[[5,7]]
</pre>

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

<pre>
<strong>输入:</strong>intervals = [[1,5]], newInterval = [2,3]
<strong>输出:</strong>[[1,5]]
</pre>

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

<pre>
<strong>输入:</strong>intervals = [[1,5]], newInterval = [2,7]
<strong>输出:</strong>[[1,7]]
<strong>解释:</strong>这是因为新的区间 <code>[4,8]</code> 与 <code>[3,5],[6,7],[8,10]</code>&nbsp;重叠。
</pre>

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

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

<ul>
<li><code>0 <= intervals.length <= 10<sup>4</sup></code></li>
<li><code>0 &lt;= intervals.length &lt;= 10<sup>4</sup></code></li>
<li><code>intervals[i].length == 2</code></li>
<li><code>0 <= intervals[i][0] <= intervals[i][1] <= 10<sup>5</sup></code></li>
<li><code>intervals</code> 根据 <code>intervals[i][0]</code> 按 <strong>升序</strong> 排列</li>
<li><code>0 &lt;=&nbsp;start<sub>i</sub> &lt;=&nbsp;end<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
<li><code>intervals</code> 根据 <code>start<sub>i</sub></code> 按 <strong>升序</strong> 排列</li>
<li><code>newInterval.length == 2</code></li>
<li><code>0 <= newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li>
<li><code>0 &lt;=&nbsp;start &lt;=&nbsp;end &lt;= 10<sup>5</sup></code></li>
</ul>

## 解法
Expand Down
2 changes: 2 additions & 0 deletions solution/0000-0099/0057.Insert Interval/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

<p>Return <code>intervals</code><em> after the insertion</em>.</p>

<p><strong>Note</strong> that you don&#39;t need to modify <code>intervals</code> in-place. You can make a new array and return it.</p>

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

Expand Down
2 changes: 1 addition & 1 deletion solution/0000-0099/0090.Subsets II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<!-- 这里写题目描述 -->

<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的&lt;span data-keyword="subset"&gt;子集&lt;/span&gt;(幂集)。</p>
<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的 <span data-keyword="subset">子集</span>(幂集)。</p>

<p>解集 <strong>不能</strong> 包含重复的子集。返回的解集中,子集可以按 <strong>任意顺序</strong> 排列。</p>

Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0310.Minimum Height Trees/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<!-- 这里写题目描述 -->

<p>树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,一个任何没有简单环路的连通图都是一棵树。</p>
<p>树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,任何一个没有简单环路的连通图都是一棵树。</p>

<p>给你一棵包含&nbsp;<code>n</code>&nbsp;个节点的树,标记为&nbsp;<code>0</code>&nbsp;到&nbsp;<code>n - 1</code> 。给定数字&nbsp;<code>n</code>&nbsp;和一个有 <code>n - 1</code> 条无向边的 <code>edges</code>&nbsp;列表(每一个边都是一对标签),其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示树中节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间存在一条无向边。</p>

Expand Down
4 changes: 2 additions & 2 deletions solution/0700-0799/0791.Custom Sort String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<p>Return <em>any permutation of </em><code>s</code><em> that satisfies this property</em>.</p>

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

<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> order = &quot;cba&quot;, s = &quot;abcd&quot; </span></p>
Expand All @@ -25,7 +25,7 @@
<p>Since <code>&quot;d&quot;</code> does not appear in <code>order</code>, it can be at any position in the returned string. <code>&quot;dcba&quot;</code>, <code>&quot;cdba&quot;</code>, <code>&quot;cbda&quot;</code> are also valid outputs.</p>
</div>

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

<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> order = &quot;bcafg&quot;, s = &quot;abcd&quot; </span></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[English Version](/solution/1000-1099/1018.Binary%20Prefix%20Divisible%20By%205/README_EN.md)

<!-- tags:数组 -->
<!-- tags:位运算,数组 -->

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[中文文档](/solution/1000-1099/1018.Binary%20Prefix%20Divisible%20By%205/README.md)

<!-- tags:Array -->
<!-- tags:Bit Manipulation,Array -->

## Description

Expand Down
2 changes: 1 addition & 1 deletion solution/1000-1099/1035.Uncrossed Lines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<p>在两条独立的水平线上按给定的顺序写下 <code>nums1</code> 和 <code>nums2</code> 中的整数。</p>

<p>现在,可以绘制一些连接两个数字 <code>nums1[i]</code>&nbsp;和 <code>nums2[j]</code>&nbsp;的直线,这些直线需要同时满足满足:</p>
<p>现在,可以绘制一些连接两个数字 <code>nums1[i]</code>&nbsp;和 <code>nums2[j]</code>&nbsp;的直线,这些直线需要同时满足:</p>

<ul>
<li>&nbsp;<code>nums1[i] == nums2[j]</code></li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,15 @@

我们先统计数组中 $1$ 的个数,记为 $k$。然后我们使用滑动窗口,窗口大小为 $k$,窗口右边界从左向右移动,统计窗口内 $1$ 的个数,记为 $t$。每次移动窗口时,都更新 $t$ 的值,最后窗口右边界移动到数组末尾时,窗口内 $1$ 的个数最多,记为 $mx$。最后答案为 $k - mx$。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def minSwaps(self, data: List[int]) -> int:
k = data.count(1)
t = sum(data[:k])
mx = t
mx = t = sum(data[:k])
for i in range(k, len(data)):
t += data[i]
t -= data[i - k]
Expand Down Expand Up @@ -159,6 +158,22 @@ function minSwaps(data: number[]): number {
}
```

```cs
public class Solution {
public int MinSwaps(int[] data) {
int k = data.Count(x => x == 1);
int t = data.Take(k).Sum();
int mx = t;
for (int i = k; i < data.Length; ++i) {
t += data[i];
t -= data[i - k];
mx = Math.Max(mx, t);
}
return k - mx;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is
class Solution:
def minSwaps(self, data: List[int]) -> int:
k = data.count(1)
t = sum(data[:k])
mx = t
mx = t = sum(data[:k])
for i in range(k, len(data)):
t += data[i]
t -= data[i - k]
Expand Down Expand Up @@ -146,6 +145,22 @@ function minSwaps(data: number[]): number {
}
```

```cs
public class Solution {
public int MinSwaps(int[] data) {
int k = data.Count(x => x == 1);
int t = data.Take(k).Sum();
int mx = t;
for (int i = k; i < data.Length; ++i) {
t += data[i];
t -= data[i - k];
mx = Math.Max(mx, t);
}
return k - mx;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Solution {
public int MinSwaps(int[] data) {
int k = data.Count(x => x == 1);
int t = data.Take(k).Sum();
int mx = t;
for (int i = k; i < data.Length; ++i) {
t += data[i];
t -= data[i - k];
mx = Math.Max(mx, t);
}
return k - mx;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Solution:
def minSwaps(self, data: List[int]) -> int:
k = data.count(1)
t = sum(data[:k])
mx = t
mx = t = sum(data[:k])
for i in range(k, len(data)):
t += data[i]
t -= data[i - k]
Expand Down
27 changes: 11 additions & 16 deletions solution/1300-1399/1361.Validate Binary Tree Nodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,28 @@

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

<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex1.png" style="height: 287px; width: 195px;"></strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex1.png" style="height: 287px; width: 195px;" /></strong></p>

<pre><strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
<pre>
<strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
<strong>输出:</strong>true
</pre>

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

<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex2.png" style="height: 272px; width: 183px;"></strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex2.png" style="height: 272px; width: 183px;" /></strong></p>

<pre><strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
<pre>
<strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
<strong>输出:</strong>false
</pre>

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

<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex3.png" style="height: 174px; width: 82px;"></strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex3.png" style="height: 174px; width: 82px;" /></strong></p>

<pre><strong>输入:</strong>n = 2, leftChild = [1,0], rightChild = [-1,-1]
<strong>输出:</strong>false
</pre>

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

<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex4.png" style="height: 191px; width: 470px;"></strong></p>

<pre><strong>输入:</strong>n = 6, leftChild = [1,-1,-1,4,-1,-1], rightChild = [2,-1,-1,5,-1,-1]
<pre>
<strong>输入:</strong>n = 2, leftChild = [1,0], rightChild = [-1,-1]
<strong>输出:</strong>false
</pre>

Expand All @@ -55,8 +50,8 @@
<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 10^4</code></li>
<li><code>leftChild.length == rightChild.length == n</code></li>
<li><code>n == leftChild.length == rightChild.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>-1 &lt;= leftChild[i], rightChild[i] &lt;= n - 1</code></li>
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<p>Given two integer arrays <code>arr1</code> and <code>arr2</code>, and the integer <code>d</code>, <em>return the distance value between the two arrays</em>.</p>

<p>The distance value is defined as the number of elements <code>arr1[i]</code> such that there is not any element <code>arr2[j]</code> where <code>|arr1[i]-arr2[j]| &lt;= d</code>.</p>
<p>The distance value is defined as the number of elements <code>arr1[i]</code> such that there is not any element <code>arr2[j]</code> where <code>|arr1[i]-arr2[j]| &lt; d</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
Loading
Loading