Skip to content

Commit e2cb9da

Browse files
authored
feat: add solutions to lc problems: No.2765~2772 (doocs#1178)
* No.2765.Longest Alternating Subarray * No.2766.Relocate Marbles * No.2767.Partition String Into Minimum Beautiful Substrings * No.2768.Number of Black Blocks * No.2769.Find the Maximum Achievable Number * No.2770.Maximum Number of Jumps to Reach the Last Index * No.2771.Longest Non-decreasing Subarray From Two Arrays * No.2772.Apply Operations to Make All Array Elements Equal to Zero
1 parent 2cb2943 commit e2cb9da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+4100
-2
lines changed

solution/1100-1199/1197.Minimum Knight Moves/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl Solution {
383383
q_from.push_back((x, y));
384384

385385
while !q_to.is_empty() && !q_from.is_empty() {
386-
let step = if q_to.len() < q_from.len() {
386+
let step = if q_to.len() < q_from.len() {
387387
Self::extend(&mut map_to, &mut map_from, &mut q_to)
388388
} else {
389389
Self::extend(&mut map_from, &mut map_to, &mut q_from)

solution/1100-1199/1197.Minimum Knight Moves/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl Solution {
362362
q_from.push_back((x, y));
363363

364364
while !q_to.is_empty() && !q_from.is_empty() {
365-
let step = if q_to.len() < q_from.len() {
365+
let step = if q_to.len() < q_from.len() {
366366
Self::extend(&mut map_to, &mut map_from, &mut q_to)
367367
} else {
368368
Self::extend(&mut map_from, &mut map_to, &mut q_from)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# [2765. 最长交替子序列](https://leetcode.cn/problems/longest-alternating-subarray)
2+
3+
[English Version](/solution/2700-2799/2765.Longest%20Alternating%20Subarray/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。如果 <code>nums</code>&nbsp;中长度为&nbsp;<code>m</code>&nbsp;的子数组&nbsp;<code>s</code>&nbsp;满足以下条件,我们称它是一个交替子序列:</p>
10+
11+
<ul>
12+
<li><code>m</code>&nbsp;大于&nbsp;<code>1</code>&nbsp;。</li>
13+
<li><code>s<sub>1</sub> = s<sub>0</sub> + 1</code>&nbsp;。</li>
14+
<li>下标从 0 开始的子数组&nbsp;<code>s</code>&nbsp;与数组&nbsp;<code>[s<sub>0</sub>, s<sub>1</sub>, s<sub>0</sub>, s<sub>1</sub>,...,s<sub>(m-1) % 2</sub>]</code>&nbsp;一样。也就是说,<code>s<sub>1</sub> - s<sub>0</sub> = 1</code>&nbsp;,<code>s<sub>2</sub> - s<sub>1</sub> = -1</code>&nbsp;,<code>s<sub>3</sub> - s<sub>2</sub> = 1</code>&nbsp;,<code>s<sub>4</sub> - s<sub>3</sub> = -1</code>&nbsp;,以此类推,直到&nbsp;<code>s[m - 1] - s[m - 2] = (-1)<sup>m</sup></code>&nbsp;。</li>
15+
</ul>
16+
17+
<p>请你返回 <code>nums</code>&nbsp;中所有 <strong>交替</strong>&nbsp;子数组中,最长的长度,如果不存在交替子数组,请你返回 <code>-1</code>&nbsp;。</p>
18+
19+
<p>子数组是一个数组中一段连续 <strong>非空</strong>&nbsp;的元素序列。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>示例 1:</strong></p>
24+
25+
<pre><b>输入:</b>nums = [2,3,4,3,4]
26+
<b>输出:</b>4
27+
<b>解释:</b>交替子数组有 [3,4] ,[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] ,长度为4 。
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre><b>输入:</b>nums = [4,5,6]
33+
<b>输出:</b>2
34+
<strong>解释:</strong>[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
39+
<p><strong>提示:</strong></p>
40+
41+
<ul>
42+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
43+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
44+
</ul>
45+
46+
## 解法
47+
48+
<!-- 这里可写通用的实现逻辑 -->
49+
50+
**方法一:枚举**
51+
52+
我们可以枚举子数组的左端点 $i$,对于每个 $i$,我们需要找到最长的满足条件的子数组。我们可以从 $i$ 开始向右遍历,每次遇到相邻元素差值不满足交替条件时,我们就找到了一个满足条件的子数组。我们可以用一个变量 $k$ 来记录当前元素的差值应该是 $1$ 还是 $-1$,如果当前元素的差值应该是 $-k$,那么我们就将 $k$ 取反。当我们找到一个满足条件的子数组 $nums[i..j]$ 时,我们更新答案为 $\max(ans, j - i + 1)$。
53+
54+
时间复杂度 $O(n^2)$,其中 $n$ 是数组的长度。我们需要枚举子数组的左端点 $i$,对于每个 $i$,我们需要 $O(n)$ 的时间来找到最长的满足条件的子数组。空间复杂度 $O(1)$。
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
class Solution:
64+
def alternatingSubarray(self, nums: List[int]) -> int:
65+
ans, n = -1, len(nums)
66+
for i in range(n):
67+
k = 1
68+
j = i
69+
while j + 1 < n and nums[j + 1] - nums[j] == k:
70+
j += 1
71+
k *= -1
72+
if j - i + 1 > 1:
73+
ans = max(ans, j - i + 1)
74+
return ans
75+
```
76+
77+
### **Java**
78+
79+
<!-- 这里可写当前语言的特殊实现逻辑 -->
80+
81+
```java
82+
class Solution {
83+
public int alternatingSubarray(int[] nums) {
84+
int ans = -1, n = nums.length;
85+
for (int i = 0; i < n; ++i) {
86+
int k = 1;
87+
int j = i;
88+
for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
89+
k *= -1;
90+
}
91+
if (j - i + 1 > 1) {
92+
ans = Math.max(ans, j - i + 1);
93+
}
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int alternatingSubarray(vector<int>& nums) {
106+
int ans = -1, n = nums.size();
107+
for (int i = 0; i < n; ++i) {
108+
int k = 1;
109+
int j = i;
110+
for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
111+
k *= -1;
112+
}
113+
if (j - i + 1 > 1) {
114+
ans = max(ans, j - i + 1);
115+
}
116+
}
117+
return ans;
118+
}
119+
};
120+
```
121+
122+
### **Go**
123+
124+
```go
125+
func alternatingSubarray(nums []int) int {
126+
ans, n := -1, len(nums)
127+
for i := range nums {
128+
k := 1
129+
j := i
130+
for ; j+1 < n && nums[j+1]-nums[j] == k; j++ {
131+
k *= -1
132+
}
133+
if t := j - i + 1; t > 1 && ans < t {
134+
ans = t
135+
}
136+
}
137+
return ans
138+
}
139+
```
140+
141+
### **TypeScript**
142+
143+
```ts
144+
function alternatingSubarray(nums: number[]): number {
145+
let ans = -1;
146+
const n = nums.length;
147+
for (let i = 0; i < n; ++i) {
148+
let k = 1;
149+
let j = i;
150+
for (; j + 1 < n && nums[j + 1] - nums[j] === k; ++j) {
151+
k *= -1;
152+
}
153+
if (j - i + 1 > 1) {
154+
ans = Math.max(ans, j - i + 1);
155+
}
156+
}
157+
return ans;
158+
}
159+
```
160+
161+
### **...**
162+
163+
```
164+
165+
```
166+
167+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# [2765. Longest Alternating Subarray](https://leetcode.com/problems/longest-alternating-subarray)
2+
3+
[中文文档](/solution/2700-2799/2765.Longest%20Alternating%20Subarray/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A subarray <code>s</code> of length <code>m</code> is called <strong>alternating</strong> if:</p>
8+
9+
<ul>
10+
<li><code>m</code> is greater than <code>1</code>.</li>
11+
<li><code>s<sub>1</sub> = s<sub>0</sub> + 1</code>.</li>
12+
<li>The 0-indexed subarray <code>s</code> looks like <code>[s<sub>0</sub>, s<sub>1</sub>, s<sub>0</sub>, s<sub>1</sub>,...,s<sub>(m-1) % 2</sub>]</code>. In other words, <code>s<sub>1</sub> - s<sub>0</sub> = 1</code>, <code>s<sub>2</sub> - s<sub>1</sub> = -1</code>, <code>s<sub>3</sub> - s<sub>2</sub> = 1</code>, <code>s<sub>4</sub> - s<sub>3</sub> = -1</code>, and so on up to <code>s[m - 1] - s[m - 2] = (-1)<sup>m</sup></code>.</li>
13+
</ul>
14+
15+
<p>Return <em>the maximum length of all <strong>alternating</strong> subarrays present in </em><code>nums</code> <em>or </em><code>-1</code><em> if no such subarray exists</em><em>.</em></p>
16+
17+
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [2,3,4,3,4]
24+
<strong>Output:</strong> 4
25+
<strong>Explanation:</strong> The alternating subarrays are [3, 4], [3, 4, 3], and [3, 4, 3, 4]. The longest of these is [3,4,3,4], which is of length 4.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [4,5,6]
32+
<strong>Output:</strong> 2
33+
<strong>Explanation:</strong> [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
41+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
42+
</ul>
43+
44+
## Solutions
45+
46+
**Solution 1: Enumeration**
47+
48+
We can enumerate the left endpoint $i$ of the subarray, and for each $i$, we need to find the longest subarray that satisfies the condition. We can start traversing to the right from $i$, and each time we encounter adjacent elements whose difference does not satisfy the alternating condition, we find a subarray that satisfies the condition. We can use a variable $k$ to record whether the difference of the current element should be $1$ or $-1$. If the difference of the current element should be $-k$, then we take the opposite of $k$. When we find a subarray $nums[i..j]$ that satisfies the condition, we update the answer to $\max(ans, j - i + 1)$.
49+
50+
The time complexity is $O(n^2)$, where $n$ is the length of the array. We need to enumerate the left endpoint $i$ of the subarray, and for each $i$, we need $O(n)$ time to find the longest subarray that satisfies the condition. The space complexity is $O(1)$.
51+
52+
<!-- tabs:start -->
53+
54+
### **Python3**
55+
56+
```python
57+
class Solution:
58+
def alternatingSubarray(self, nums: List[int]) -> int:
59+
ans, n = -1, len(nums)
60+
for i in range(n):
61+
k = 1
62+
j = i
63+
while j + 1 < n and nums[j + 1] - nums[j] == k:
64+
j += 1
65+
k *= -1
66+
if j - i + 1 > 1:
67+
ans = max(ans, j - i + 1)
68+
return ans
69+
```
70+
71+
### **Java**
72+
73+
```java
74+
class Solution {
75+
public int alternatingSubarray(int[] nums) {
76+
int ans = -1, n = nums.length;
77+
for (int i = 0; i < n; ++i) {
78+
int k = 1;
79+
int j = i;
80+
for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
81+
k *= -1;
82+
}
83+
if (j - i + 1 > 1) {
84+
ans = Math.max(ans, j - i + 1);
85+
}
86+
}
87+
return ans;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int alternatingSubarray(vector<int>& nums) {
98+
int ans = -1, n = nums.size();
99+
for (int i = 0; i < n; ++i) {
100+
int k = 1;
101+
int j = i;
102+
for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
103+
k *= -1;
104+
}
105+
if (j - i + 1 > 1) {
106+
ans = max(ans, j - i + 1);
107+
}
108+
}
109+
return ans;
110+
}
111+
};
112+
```
113+
114+
### **Go**
115+
116+
```go
117+
func alternatingSubarray(nums []int) int {
118+
ans, n := -1, len(nums)
119+
for i := range nums {
120+
k := 1
121+
j := i
122+
for ; j+1 < n && nums[j+1]-nums[j] == k; j++ {
123+
k *= -1
124+
}
125+
if t := j - i + 1; t > 1 && ans < t {
126+
ans = t
127+
}
128+
}
129+
return ans
130+
}
131+
```
132+
133+
### **TypeScript**
134+
135+
```ts
136+
function alternatingSubarray(nums: number[]): number {
137+
let ans = -1;
138+
const n = nums.length;
139+
for (let i = 0; i < n; ++i) {
140+
let k = 1;
141+
let j = i;
142+
for (; j + 1 < n && nums[j + 1] - nums[j] === k; ++j) {
143+
k *= -1;
144+
}
145+
if (j - i + 1 > 1) {
146+
ans = Math.max(ans, j - i + 1);
147+
}
148+
}
149+
return ans;
150+
}
151+
```
152+
153+
### **...**
154+
155+
```
156+
157+
```
158+
159+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int alternatingSubarray(vector<int>& nums) {
4+
int ans = -1, n = nums.size();
5+
for (int i = 0; i < n; ++i) {
6+
int k = 1;
7+
int j = i;
8+
for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
9+
k *= -1;
10+
}
11+
if (j - i + 1 > 1) {
12+
ans = max(ans, j - i + 1);
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func alternatingSubarray(nums []int) int {
2+
ans, n := -1, len(nums)
3+
for i := range nums {
4+
k := 1
5+
j := i
6+
for ; j+1 < n && nums[j+1]-nums[j] == k; j++ {
7+
k *= -1
8+
}
9+
if t := j - i + 1; t > 1 && ans < t {
10+
ans = t
11+
}
12+
}
13+
return ans
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int alternatingSubarray(int[] nums) {
3+
int ans = -1, n = nums.length;
4+
for (int i = 0; i < n; ++i) {
5+
int k = 1;
6+
int j = i;
7+
for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
8+
k *= -1;
9+
}
10+
if (j - i + 1 > 1) {
11+
ans = Math.max(ans, j - i + 1);
12+
}
13+
}
14+
return ans;
15+
}
16+
}

0 commit comments

Comments
 (0)