Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:一次遍历

根据题目描述,我们只需要找到最大的相邻递增子数组长度 $\textit{mx}$,如果 $\textit{mx} \ge k$,则说明存在两个相邻且长度为 $k$ 的严格递增子数组。

我们可以使用一次遍历来计算 $\textit{mx}$。具体来说,我们维护三个变量,其中 $\textit{cur}$ 和 $\textit{pre}$ 分别表示当前递增子数组的长度和前一个递增子数组的长度,而 $\textit{mx}$ 表示最大的相邻递增子数组长度。

每当遇到一个非递增的位置时,我们就更新 $\textit{mx}$,并将 $\textit{cur}$ 赋值给 $\textit{pre}$,然后将 $\textit{cur}$ 重置为 $0$,其中 $\textit{mx}$ 的更新方式为 $\textit{mx} = \max(\textit{mx}, \lfloor \frac{\textit{cur}}{2} \rfloor, \min(\textit{pre}, \text{cur}))$,即相邻递增子数组来自于当前递增子数组的一半长度,或者前一个递增子数组和当前递增子数组的较小值。

最后,我们只需要判断 $\textit{mx}$ 是否大于等于 $k$ 即可。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -163,6 +173,51 @@ function hasIncreasingSubarrays(nums: number[], k: number): boolean {
}
```

#### Rust

```rust
impl Solution {
pub fn has_increasing_subarrays(nums: Vec<i32>, k: i32) -> bool {
let n = nums.len();
let (mut mx, mut pre, mut cur) = (0, 0, 0);

for i in 0..n {
cur += 1;
if i == n - 1 || nums[i] >= nums[i + 1] {
mx = mx.max(cur / 2).max(pre.min(cur));
pre = cur;
cur = 0;
}
}

mx >= k
}
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var hasIncreasingSubarrays = function (nums, k) {
const n = nums.length;
let [mx, pre, cur] = [0, 0, 0];
for (let i = 0; i < n; ++i) {
++cur;
if (i === n - 1 || nums[i] >= nums[i + 1]) {
mx = Math.max(mx, cur >> 1, Math.min(pre, cur));
pre = cur;
cur = 0;
}
}
return mx >= k;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Single Pass

According to the problem description, we only need to find the maximum length of adjacent increasing subarrays $\textit{mx}$. If $\textit{mx} \ge k$, then there exist two adjacent strictly increasing subarrays of length $k$.

We can use a single pass to calculate $\textit{mx}$. Specifically, we maintain three variables: $\textit{cur}$ and $\textit{pre}$ represent the length of the current increasing subarray and the previous increasing subarray respectively, while $\textit{mx}$ represents the maximum length of adjacent increasing subarrays.

Whenever we encounter a non-increasing position, we update $\textit{mx}$, assign $\textit{cur}$ to $\textit{pre}$, and reset $\textit{cur}$ to $0$. The update formula for $\textit{mx}$ is $\textit{mx} = \max(\textit{mx}, \lfloor \frac{\textit{cur}}{2} \rfloor, \min(\textit{pre}, \textit{cur}))$, meaning the adjacent increasing subarrays come from either half the length of the current increasing subarray, or the smaller value between the previous increasing subarray and the current increasing subarray.

Finally, we just need to check whether $\textit{mx}$ is greater than or equal to $k$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -159,6 +169,51 @@ function hasIncreasingSubarrays(nums: number[], k: number): boolean {
}
```

#### Rust

```rust
impl Solution {
pub fn has_increasing_subarrays(nums: Vec<i32>, k: i32) -> bool {
let n = nums.len();
let (mut mx, mut pre, mut cur) = (0, 0, 0);

for i in 0..n {
cur += 1;
if i == n - 1 || nums[i] >= nums[i + 1] {
mx = mx.max(cur / 2).max(pre.min(cur));
pre = cur;
cur = 0;
}
}

mx >= k
}
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var hasIncreasingSubarrays = function (nums, k) {
const n = nums.length;
let [mx, pre, cur] = [0, 0, 0];
for (let i = 0; i < n; ++i) {
++cur;
if (i === n - 1 || nums[i] >= nums[i + 1]) {
mx = Math.max(mx, cur >> 1, Math.min(pre, cur));
pre = cur;
cur = 0;
}
}
return mx >= k;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var hasIncreasingSubarrays = function (nums, k) {
const n = nums.length;
let [mx, pre, cur] = [0, 0, 0];
for (let i = 0; i < n; ++i) {
++cur;
if (i === n - 1 || nums[i] >= nums[i + 1]) {
mx = Math.max(mx, cur >> 1, Math.min(pre, cur));
pre = cur;
cur = 0;
}
}
return mx >= k;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl Solution {
pub fn has_increasing_subarrays(nums: Vec<i32>, k: i32) -> bool {
let n = nums.len();
let (mut mx, mut pre, mut cur) = (0, 0, 0);

for i in 0..n {
cur += 1;
if i == n - 1 || nums[i] >= nums[i + 1] {
mx = mx.max(cur / 2).max(pre.min(cur));
pre = cur;
cur = 0;
}
}

mx >= k
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:一次遍历

我们可以使用一次遍历来计算最大的相邻递增子数组长度 $\textit{ans}$。具体地,我们维护三个变量 $\textit{cur}$ 和 $\textit{pre}$ 分别表示当前递增子数组和上一个递增子数组的长度,而 $\textit{ans}$ 表示最大的相邻递增子数组长度。

每当遇到一个非递增的位置时,我们就更新 $\textit{ans}$,将 $\textit{cur}$ 赋值给 $\textit{pre}$,并将 $\textit{cur}$ 重置为 $0$。更新 $\textit{ans}$ 的公式为 $\textit{ans} = \max(\textit{ans}, \lfloor \frac{\textit{cur}}{2} \rfloor, \min(\textit{pre}, \textit{cur}))$,表示相邻递增子数组要么来自当前递增子数组长度的一半,要么来自前一个递增子数组和当前递增子数组的较小值。

最后我们只需要返回 $\textit{ans}$ 即可。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -171,6 +179,49 @@ function maxIncreasingSubarrays(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_increasing_subarrays(nums: Vec<i32>) -> i32 {
let n = nums.len();
let (mut ans, mut pre, mut cur) = (0, 0, 0);

for i in 0..n {
cur += 1;
if i == n - 1 || nums[i] >= nums[i + 1] {
ans = ans.max(cur / 2).max(pre.min(cur));
pre = cur;
cur = 0;
}
}

ans
}
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @return {number}
*/
var maxIncreasingSubarrays = function (nums) {
let [ans, pre, cur] = [0, 0, 0];
const n = nums.length;
for (let i = 0; i < n; ++i) {
++cur;
if (i === n - 1 || nums[i] >= nums[i + 1]) {
ans = Math.max(ans, cur >> 1, Math.min(pre, cur));
[pre, cur] = [cur, 0];
}
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Single Pass

We can use a single pass to calculate the maximum length of adjacent increasing subarrays $\textit{ans}$. Specifically, we maintain three variables: $\textit{cur}$ and $\textit{pre}$ represent the length of the current increasing subarray and the previous increasing subarray respectively, while $\textit{ans}$ represents the maximum length of adjacent increasing subarrays.

Whenever we encounter a non-increasing position, we update $\textit{ans}$, assign $\textit{cur}$ to $\textit{pre}$, and reset $\textit{cur}$ to $0$. The update formula for $\textit{ans}$ is $\textit{ans} = \max(\textit{ans}, \lfloor \frac{\textit{cur}}{2} \rfloor, \min(\textit{pre}, \textit{cur}))$, meaning the adjacent increasing subarrays come from either half the length of the current increasing subarray, or the smaller value between the previous increasing subarray and the current increasing subarray.

Finally, we just need to return $\textit{ans}$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -169,6 +177,49 @@ function maxIncreasingSubarrays(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_increasing_subarrays(nums: Vec<i32>) -> i32 {
let n = nums.len();
let (mut ans, mut pre, mut cur) = (0, 0, 0);

for i in 0..n {
cur += 1;
if i == n - 1 || nums[i] >= nums[i + 1] {
ans = ans.max(cur / 2).max(pre.min(cur));
pre = cur;
cur = 0;
}
}

ans
}
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @return {number}
*/
var maxIncreasingSubarrays = function (nums) {
let [ans, pre, cur] = [0, 0, 0];
const n = nums.length;
for (let i = 0; i < n; ++i) {
++cur;
if (i === n - 1 || nums[i] >= nums[i + 1]) {
ans = Math.max(ans, cur >> 1, Math.min(pre, cur));
[pre, cur] = [cur, 0];
}
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @param {number[]} nums
* @return {number}
*/
var maxIncreasingSubarrays = function (nums) {
let [ans, pre, cur] = [0, 0, 0];
const n = nums.length;
for (let i = 0; i < n; ++i) {
++cur;
if (i === n - 1 || nums[i] >= nums[i + 1]) {
ans = Math.max(ans, cur >> 1, Math.min(pre, cur));
[pre, cur] = [cur, 0];
}
}
return ans;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl Solution {
pub fn max_increasing_subarrays(nums: Vec<i32>) -> i32 {
let n = nums.len();
let (mut ans, mut pre, mut cur) = (0, 0, 0);

for i in 0..n {
cur += 1;
if i == n - 1 || nums[i] >= nums[i + 1] {
ans = ans.max(cur / 2).max(pre.min(cur));
pre = cur;
cur = 0;
}
}

ans
}
}