Skip to content

Commit ba5ae23

Browse files
committed
feat: add solutions to lc problems: No.2765~2772
* 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 ba5ae23

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

+3999
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)
Lines changed: 167 additions & 0 deletions
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 -->
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
<!-- tabs:start -->
47+
48+
### **Python3**
49+
50+
```python
51+
class Solution:
52+
def alternatingSubarray(self, nums: List[int]) -> int:
53+
ans, n = -1, len(nums)
54+
for i in range(n):
55+
k = 1
56+
j = i
57+
while j + 1 < n and nums[j + 1] - nums[j] == k:
58+
j += 1
59+
k *= -1
60+
if j - i + 1 > 1:
61+
ans = max(ans, j - i + 1)
62+
return ans
63+
```
64+
65+
### **Java**
66+
67+
```java
68+
class Solution {
69+
public int alternatingSubarray(int[] nums) {
70+
int ans = -1, n = nums.length;
71+
for (int i = 0; i < n; ++i) {
72+
int k = 1;
73+
int j = i;
74+
for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
75+
k *= -1;
76+
}
77+
if (j - i + 1 > 1) {
78+
ans = Math.max(ans, j - i + 1);
79+
}
80+
}
81+
return ans;
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int alternatingSubarray(vector<int>& nums) {
92+
int ans = -1, n = nums.size();
93+
for (int i = 0; i < n; ++i) {
94+
int k = 1;
95+
int j = i;
96+
for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
97+
k *= -1;
98+
}
99+
if (j - i + 1 > 1) {
100+
ans = max(ans, j - i + 1);
101+
}
102+
}
103+
return ans;
104+
}
105+
};
106+
```
107+
108+
### **Go**
109+
110+
```go
111+
func alternatingSubarray(nums []int) int {
112+
ans, n := -1, len(nums)
113+
for i := range nums {
114+
k := 1
115+
j := i
116+
for ; j+1 < n && nums[j+1]-nums[j] == k; j++ {
117+
k *= -1
118+
}
119+
if t := j - i + 1; t > 1 && ans < t {
120+
ans = t
121+
}
122+
}
123+
return ans
124+
}
125+
```
126+
127+
### **TypeScript**
128+
129+
```ts
130+
function alternatingSubarray(nums: number[]): number {
131+
let ans = -1;
132+
const n = nums.length;
133+
for (let i = 0; i < n; ++i) {
134+
let k = 1;
135+
let j = i;
136+
for (; j + 1 < n && nums[j + 1] - nums[j] === k; ++j) {
137+
k *= -1;
138+
}
139+
if (j - i + 1 > 1) {
140+
ans = Math.max(ans, j - i + 1);
141+
}
142+
}
143+
return ans;
144+
}
145+
```
146+
147+
### **...**
148+
149+
```
150+
151+
```
152+
153+
<!-- tabs:end -->
Lines changed: 17 additions & 0 deletions
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+
};
Lines changed: 14 additions & 0 deletions
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+
}
Lines changed: 16 additions & 0 deletions
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)