Skip to content

[pull] main from doocs:main #182

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 7 commits into from
Sep 24, 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
52 changes: 28 additions & 24 deletions solution/0600-0699/0621.Task Scheduler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,37 @@ tags:

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

<pre>
<strong>输入:</strong>tasks = ["A","A","A","B","B","B"], n = 2
<strong>输出:</strong>8
<strong>解释:</strong>A -&gt; B -&gt; (待命) -&gt; A -&gt; B -&gt; (待命) -&gt; A -&gt; B
在本示例中,两个相同类型任务之间必须间隔长度为 n = 2 的冷却时间,而执行一个任务只需要一个单位时间,所以中间出现了(待命)状态。 </pre>

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

<pre>
<strong>输入:</strong>tasks = ["A","A","A","B","B","B"], n = 0
<strong>输出:</strong>6
<strong>解释:</strong>在这种情况下,任何大小为 6 的排列都可以满足要求,因为 n = 0
["A","A","A","B","B","B"]
["A","B","A","B","A","B"]
["B","B","B","A","A","A"]
...
诸如此类
</pre>
<div class="example-block"><strong>输入:</strong>tasks = ["A","A","A","B","B","B"], n = 2</div>

<div class="example-block"><strong>输出:</strong>8</div>

<div class="example-block"><strong>解释:</strong></div>

<div class="example-block">在完成任务 A 之后,你必须等待两个间隔。对任务 B 来说也是一样。在第 3 个间隔,A 和 B 都不能完成,所以你需要待命。在第 4 个间隔,由于已经经过了 2 个间隔,你可以再次执行 A 任务。</div>

<div class="example-block">&nbsp;</div>

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

<div class="example-block">
<p><b>输入:</b>tasks = ["A","C","A","B","D","B"], n = 1</p>

<p><b>输出:</b>6</p>

<p><b>解释:</b>一种可能的序列是:A -&gt; B -&gt; C -&gt; D -&gt; A -&gt; B。</p>

<p>由于冷却间隔为 1,你可以在完成另一个任务后重复执行这个任务。</p>
</div>

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

<pre>
<strong>输入:</strong>tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
<strong>输出:</strong>16
<strong>解释:</strong>一种可能的解决方案是:
A -&gt; B -&gt; C -&gt; A -&gt; D -&gt; E -&gt; A -&gt; F -&gt; G -&gt; A -&gt; (待命) -&gt; (待命) -&gt; A -&gt; (待命) -&gt; (待命) -&gt; A
</pre>
<div class="example-block"><strong>输入:</strong>tasks = ["A","A","A","B","B","B"], n = 0</div>

<div class="example-block"><strong>输出:</strong>6</div>

<div class="example-block"><strong>解释:</strong>一种可能的序列为:A -&gt; B -&gt; idle -&gt; idle -&gt; A -&gt; B -&gt; idle -&gt; idle -&gt; A -&gt; B。</div>

<div class="example-block">只有两种任务类型,A 和 B,需要被 3 个间隔分割。这导致重复执行这些任务的间隔当中有两次待命状态。</div>

<p>&nbsp;</p>

Expand Down
6 changes: 3 additions & 3 deletions solution/0600-0699/0621.Task Scheduler/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ tags:

<!-- description:start -->

<p>You are given an array of CPU <code>tasks</code>, each represented by letters&nbsp;A&nbsp;to Z, and a cooling time, <code>n</code>. Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there&#39;s a constraint: <strong>identical</strong> tasks must be separated by at least <code>n</code> intervals due to cooling time.</p>
<p>You are given an array of CPU <code>tasks</code>, each labeled with a letter from A to Z, and a number <code>n</code>. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there&#39;s a constraint: there has to be a gap of <strong>at least</strong> <code>n</code> intervals between two tasks with the same label.</p>

<p>Return the <em>minimum number of intervals</em> required to complete all tasks.</p>
<p>Return the <strong>minimum</strong> number of CPU intervals required to complete all tasks.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand All @@ -50,7 +50,7 @@ font-size: 0.85rem;

<p><strong>Explanation:</strong> A possible sequence is: A -&gt; B -&gt; idle -&gt; A -&gt; B -&gt; idle -&gt; A -&gt; B.</p>

<p>After completing task A, you must wait two cycles before doing A again. The same applies to task B. In the 3<sup>rd</sup> interval, neither A nor B can be done, so you idle. By the 4<sup>th</sup> cycle, you can do A again as 2 intervals have passed.</p>
<p>After completing task A, you must wait two intervals before doing A again. The same applies to task B. In the 3<sup>rd</sup> interval, neither A nor B can be done, so you idle. By the 4<sup>th</sup> interval, you can do A again as 2 intervals have passed.</p>
</div>

<p><strong class="example">Example 2:</strong></p>
Expand Down
4 changes: 2 additions & 2 deletions solution/0600-0699/0658.Find K Closest Elements/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ tags:
<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>arr = [1,2,3,4,5], k = 4, x = -1
<strong>输出:</strong>[1,2,3,4]
<strong>输入:</strong>arr = [1,1,2,3,4,5], k = 4, x = -1
<strong>输出:</strong>[1,1,2,3]
</pre>

<p>&nbsp;</p>
Expand Down
56 changes: 36 additions & 20 deletions solution/1000-1099/1014.Best Sightseeing Pair/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ tags:

<!-- solution:start -->

### 方法一:枚举 + 维护前缀最大值
### 方法一:枚举

我们可以在 $[1,..n - 1]$ 的范围内枚举 $j$,那么我们要在 $[0,..j - 1]$ 的范围内找到一个 $i$,使得 $values[i] + values[j] + i - j$ 的值最大。我们可以维护一个前缀最大值,即 $values[i] + i$ 的最大值,那么我们只需要在枚举 $j$ 的过程中,不断地更新答案即可
我们可以从左到右枚举 $j$,同时维护 $j$ 左侧元素中 $values[i] + i$ 的最大值 $mx$,这样对于每个 $j$,最大得分为 $mx + values[j] - j$。我们取所有位置的最大得分的最大值即为答案

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{values}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -70,10 +70,10 @@ tags:
```python
class Solution:
def maxScoreSightseeingPair(self, values: List[int]) -> int:
ans, mx = 0, values[0]
for j in range(1, len(values)):
ans = max(ans, values[j] - j + mx)
mx = max(mx, values[j] + j)
ans = mx = 0
for j, x in enumerate(values):
ans = max(ans, mx + x - j)
mx = max(mx, x + j)
return ans
```

Expand All @@ -82,9 +82,9 @@ class Solution:
```java
class Solution {
public int maxScoreSightseeingPair(int[] values) {
int ans = 0, mx = values[0];
for (int j = 1; j < values.length; ++j) {
ans = Math.max(ans, values[j] - j + mx);
int ans = 0, mx = 0;
for (int j = 0; j < values.length; ++j) {
ans = Math.max(ans, mx + values[j] - j);
mx = Math.max(mx, values[j] + j);
}
return ans;
Expand All @@ -98,9 +98,9 @@ class Solution {
class Solution {
public:
int maxScoreSightseeingPair(vector<int>& values) {
int ans = 0, mx = values[0];
for (int j = 1; j < values.size(); ++j) {
ans = max(ans, values[j] - j + mx);
int ans = 0, mx = 0;
for (int j = 0; j < values.size(); ++j) {
ans = max(ans, mx + values[j] - j);
mx = max(mx, values[j] + j);
}
return ans;
Expand All @@ -112,9 +112,10 @@ public:

```go
func maxScoreSightseeingPair(values []int) (ans int) {
for j, mx := 1, values[0]; j < len(values); j++ {
ans = max(ans, values[j]-j+mx)
mx = max(mx, values[j]+j)
mx := 0
for j, x := range values {
ans = max(ans, mx+x-j)
mx = max(mx, x+j)
}
return
}
Expand All @@ -124,16 +125,31 @@ func maxScoreSightseeingPair(values []int) (ans int) {

```ts
function maxScoreSightseeingPair(values: number[]): number {
let ans = 0;
let mx = values[0];
for (let j = 1; j < values.length; ++j) {
ans = Math.max(ans, values[j] - j + mx);
let [ans, mx] = [0, 0];
for (let j = 0; j < values.length; ++j) {
ans = Math.max(ans, mx + values[j] - j);
mx = Math.max(mx, values[j] + j);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn max_score_sightseeing_pair(values: Vec<i32>) -> i32 {
let mut ans = 0;
let mut mx = 0;
for (j, &x) in values.iter().enumerate() {
ans = ans.max(mx + x - j as i32);
mx = mx.max(x + j as i32);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
56 changes: 38 additions & 18 deletions solution/1000-1099/1014.Best Sightseeing Pair/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration

We can enumerate $j$ from left to right while maintaining the maximum value of $values[i] + i$ for elements to the left of $j$, denoted as $mx$. For each $j$, the maximum score is $mx + values[j] - j$. The answer is the maximum of these maximum scores for all positions.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{values}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -64,10 +68,10 @@ tags:
```python
class Solution:
def maxScoreSightseeingPair(self, values: List[int]) -> int:
ans, mx = 0, values[0]
for j in range(1, len(values)):
ans = max(ans, values[j] - j + mx)
mx = max(mx, values[j] + j)
ans = mx = 0
for j, x in enumerate(values):
ans = max(ans, mx + x - j)
mx = max(mx, x + j)
return ans
```

Expand All @@ -76,9 +80,9 @@ class Solution:
```java
class Solution {
public int maxScoreSightseeingPair(int[] values) {
int ans = 0, mx = values[0];
for (int j = 1; j < values.length; ++j) {
ans = Math.max(ans, values[j] - j + mx);
int ans = 0, mx = 0;
for (int j = 0; j < values.length; ++j) {
ans = Math.max(ans, mx + values[j] - j);
mx = Math.max(mx, values[j] + j);
}
return ans;
Expand All @@ -92,9 +96,9 @@ class Solution {
class Solution {
public:
int maxScoreSightseeingPair(vector<int>& values) {
int ans = 0, mx = values[0];
for (int j = 1; j < values.size(); ++j) {
ans = max(ans, values[j] - j + mx);
int ans = 0, mx = 0;
for (int j = 0; j < values.size(); ++j) {
ans = max(ans, mx + values[j] - j);
mx = max(mx, values[j] + j);
}
return ans;
Expand All @@ -106,9 +110,10 @@ public:

```go
func maxScoreSightseeingPair(values []int) (ans int) {
for j, mx := 1, values[0]; j < len(values); j++ {
ans = max(ans, values[j]-j+mx)
mx = max(mx, values[j]+j)
mx := 0
for j, x := range values {
ans = max(ans, mx+x-j)
mx = max(mx, x+j)
}
return
}
Expand All @@ -118,16 +123,31 @@ func maxScoreSightseeingPair(values []int) (ans int) {

```ts
function maxScoreSightseeingPair(values: number[]): number {
let ans = 0;
let mx = values[0];
for (let j = 1; j < values.length; ++j) {
ans = Math.max(ans, values[j] - j + mx);
let [ans, mx] = [0, 0];
for (let j = 0; j < values.length; ++j) {
ans = Math.max(ans, mx + values[j] - j);
mx = Math.max(mx, values[j] + j);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn max_score_sightseeing_pair(values: Vec<i32>) -> i32 {
let mut ans = 0;
let mut mx = 0;
for (j, &x) in values.iter().enumerate() {
ans = ans.max(mx + x - j as i32);
mx = mx.max(x + j as i32);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
8 changes: 4 additions & 4 deletions solution/1000-1099/1014.Best Sightseeing Pair/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution {
public:
int maxScoreSightseeingPair(vector<int>& values) {
int ans = 0, mx = values[0];
for (int j = 1; j < values.size(); ++j) {
ans = max(ans, values[j] - j + mx);
int ans = 0, mx = 0;
for (int j = 0; j < values.size(); ++j) {
ans = max(ans, mx + values[j] - j);
mx = max(mx, values[j] + j);
}
return ans;
}
};
};
9 changes: 5 additions & 4 deletions solution/1000-1099/1014.Best Sightseeing Pair/Solution.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
func maxScoreSightseeingPair(values []int) (ans int) {
for j, mx := 1, values[0]; j < len(values); j++ {
ans = max(ans, values[j]-j+mx)
mx = max(mx, values[j]+j)
mx := 0
for j, x := range values {
ans = max(ans, mx+x-j)
mx = max(mx, x+j)
}
return
}
}
8 changes: 4 additions & 4 deletions solution/1000-1099/1014.Best Sightseeing Pair/Solution.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Solution {
public int maxScoreSightseeingPair(int[] values) {
int ans = 0, mx = values[0];
for (int j = 1; j < values.length; ++j) {
ans = Math.max(ans, values[j] - j + mx);
int ans = 0, mx = 0;
for (int j = 0; j < values.length; ++j) {
ans = Math.max(ans, mx + values[j] - j);
mx = Math.max(mx, values[j] + j);
}
return ans;
}
}
}
8 changes: 4 additions & 4 deletions solution/1000-1099/1014.Best Sightseeing Pair/Solution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution:
def maxScoreSightseeingPair(self, values: List[int]) -> int:
ans, mx = 0, values[0]
for j in range(1, len(values)):
ans = max(ans, values[j] - j + mx)
mx = max(mx, values[j] + j)
ans = mx = 0
for j, x in enumerate(values):
ans = max(ans, mx + x - j)
mx = max(mx, x + j)
return ans
11 changes: 11 additions & 0 deletions solution/1000-1099/1014.Best Sightseeing Pair/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
impl Solution {
pub fn max_score_sightseeing_pair(values: Vec<i32>) -> i32 {
let mut ans = 0;
let mut mx = 0;
for (j, &x) in values.iter().enumerate() {
ans = ans.max(mx + x - j as i32);
mx = mx.max(x + j as i32);
}
ans
}
}
7 changes: 3 additions & 4 deletions solution/1000-1099/1014.Best Sightseeing Pair/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
function maxScoreSightseeingPair(values: number[]): number {
let ans = 0;
let mx = values[0];
for (let j = 1; j < values.length; ++j) {
ans = Math.max(ans, values[j] - j + mx);
let [ans, mx] = [0, 0];
for (let j = 0; j < values.length; ++j) {
ans = Math.max(ans, mx + values[j] - j);
mx = Math.max(mx, values[j] + j);
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ tags:

### 方法一:模拟

遍历数组,每一次遍历都将当前数字加到前面的数字上,然后对 $5$ 取模,如果结果为 $0$,则当前数字可以被 $5$ 整除,答案设置为 `true`,否则为 `false`
我们用一个变量 $x$ 来表示当前的二进制前缀,然后遍历数组 $nums$,对于每个元素 $v$,我们将 $x$ 左移一位,然后加上 $v$,再对 $5$ 取模,判断是否等于 $0$,如果等于 $0$,则说明当前的二进制前缀可以被 $5$ 整除,我们将 $\textit{true}$ 加入答案数组,否则将 $\textit{false}$ 加入答案数组

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
时间复杂度 $O(n)$,忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down
Loading