Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e1ea38

Browse files
feat: add solutions to lc problems: No.3349,3350 (#4786)
1 parent 2139acd commit 0e1ea38

File tree

8 files changed

+284
-4
lines changed

8 files changed

+284
-4
lines changed

‎solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README.md‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,17 @@ tags:
7171

7272
<!-- solution:start -->
7373

74-
### 方法一
74+
### 方法一:一次遍历
75+
76+
根据题目描述,我们只需要找到最大的相邻递增子数组长度 $\textit{mx},ドル如果 $\textit{mx} \ge k,ドル则说明存在两个相邻且长度为 $k$ 的严格递增子数组。
77+
78+
我们可以使用一次遍历来计算 $\textit{mx}$。具体来说,我们维护三个变量,其中 $\textit{cur}$ 和 $\textit{pre}$ 分别表示当前递增子数组的长度和前一个递增子数组的长度,而 $\textit{mx}$ 表示最大的相邻递增子数组长度。
79+
80+
每当遇到一个非递增的位置时,我们就更新 $\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})),ドル即相邻递增子数组来自于当前递增子数组的一半长度,或者前一个递增子数组和当前递增子数组的较小值。
81+
82+
最后,我们只需要判断 $\textit{mx}$ 是否大于等于 $k$ 即可。
83+
84+
时间复杂度 $O(n),ドル其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
7585

7686
<!-- tabs:start -->
7787

@@ -163,6 +173,51 @@ function hasIncreasingSubarrays(nums: number[], k: number): boolean {
163173
}
164174
```
165175

176+
#### Rust
177+
178+
```rust
179+
impl Solution {
180+
pub fn has_increasing_subarrays(nums: Vec<i32>, k: i32) -> bool {
181+
let n = nums.len();
182+
let (mut mx, mut pre, mut cur) = (0, 0, 0);
183+
184+
for i in 0..n {
185+
cur += 1;
186+
if i == n - 1 || nums[i] >= nums[i + 1] {
187+
mx = mx.max(cur / 2).max(pre.min(cur));
188+
pre = cur;
189+
cur = 0;
190+
}
191+
}
192+
193+
mx >= k
194+
}
195+
}
196+
```
197+
198+
#### JavaScript
199+
200+
```js
201+
/**
202+
* @param {number[]} nums
203+
* @param {number} k
204+
* @return {boolean}
205+
*/
206+
var hasIncreasingSubarrays = function (nums, k) {
207+
const n = nums.length;
208+
let [mx, pre, cur] = [0, 0, 0];
209+
for (let i = 0; i < n; ++i) {
210+
++cur;
211+
if (i === n - 1 || nums[i] >= nums[i + 1]) {
212+
mx = Math.max(mx, cur >> 1, Math.min(pre, cur));
213+
pre = cur;
214+
cur = 0;
215+
}
216+
}
217+
return mx >= k;
218+
};
219+
```
220+
166221
<!-- tabs:end -->
167222

168223
<!-- solution:end -->

‎solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README_EN.md‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Single Pass
71+
72+
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$.
73+
74+
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.
75+
76+
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.
77+
78+
Finally, we just need to check whether $\textit{mx}$ is greater than or equal to $k$.
79+
80+
The time complexity is $O(n),ドル where $n$ is the length of the array. The space complexity is $O(1)$.
7181

7282
<!-- tabs:start -->
7383

@@ -159,6 +169,51 @@ function hasIncreasingSubarrays(nums: number[], k: number): boolean {
159169
}
160170
```
161171

172+
#### Rust
173+
174+
```rust
175+
impl Solution {
176+
pub fn has_increasing_subarrays(nums: Vec<i32>, k: i32) -> bool {
177+
let n = nums.len();
178+
let (mut mx, mut pre, mut cur) = (0, 0, 0);
179+
180+
for i in 0..n {
181+
cur += 1;
182+
if i == n - 1 || nums[i] >= nums[i + 1] {
183+
mx = mx.max(cur / 2).max(pre.min(cur));
184+
pre = cur;
185+
cur = 0;
186+
}
187+
}
188+
189+
mx >= k
190+
}
191+
}
192+
```
193+
194+
#### JavaScript
195+
196+
```js
197+
/**
198+
* @param {number[]} nums
199+
* @param {number} k
200+
* @return {boolean}
201+
*/
202+
var hasIncreasingSubarrays = function (nums, k) {
203+
const n = nums.length;
204+
let [mx, pre, cur] = [0, 0, 0];
205+
for (let i = 0; i < n; ++i) {
206+
++cur;
207+
if (i === n - 1 || nums[i] >= nums[i + 1]) {
208+
mx = Math.max(mx, cur >> 1, Math.min(pre, cur));
209+
pre = cur;
210+
cur = 0;
211+
}
212+
}
213+
return mx >= k;
214+
};
215+
```
216+
162217
<!-- tabs:end -->
163218

164219
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {boolean}
5+
*/
6+
var hasIncreasingSubarrays = function (nums, k) {
7+
const n = nums.length;
8+
let [mx, pre, cur] = [0, 0, 0];
9+
for (let i = 0; i < n; ++i) {
10+
++cur;
11+
if (i === n - 1 || nums[i] >= nums[i + 1]) {
12+
mx = Math.max(mx, cur >> 1, Math.min(pre, cur));
13+
pre = cur;
14+
cur = 0;
15+
}
16+
}
17+
return mx >= k;
18+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn has_increasing_subarrays(nums: Vec<i32>, k: i32) -> bool {
3+
let n = nums.len();
4+
let (mut mx, mut pre, mut cur) = (0, 0, 0);
5+
6+
for i in 0..n {
7+
cur += 1;
8+
if i == n - 1 || nums[i] >= nums[i + 1] {
9+
mx = mx.max(cur / 2).max(pre.min(cur));
10+
pre = cur;
11+
cur = 0;
12+
}
13+
}
14+
15+
mx >= k
16+
}
17+
}

‎solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README.md‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ tags:
7979

8080
<!-- solution:start -->
8181

82-
### 方法一
82+
### 方法一:一次遍历
83+
84+
我们可以使用一次遍历来计算最大的相邻递增子数组长度 $\textit{ans}$。具体地,我们维护三个变量 $\textit{cur}$ 和 $\textit{pre}$ 分别表示当前递增子数组和上一个递增子数组的长度,而 $\textit{ans}$ 表示最大的相邻递增子数组长度。
85+
86+
每当遇到一个非递增的位置时,我们就更新 $\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})),ドル表示相邻递增子数组要么来自当前递增子数组长度的一半,要么来自前一个递增子数组和当前递增子数组的较小值。
87+
88+
最后我们只需要返回 $\textit{ans}$ 即可。
89+
90+
时间复杂度 $O(n),ドル其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
8391

8492
<!-- tabs:start -->
8593

@@ -171,6 +179,49 @@ function maxIncreasingSubarrays(nums: number[]): number {
171179
}
172180
```
173181

182+
#### Rust
183+
184+
```rust
185+
impl Solution {
186+
pub fn max_increasing_subarrays(nums: Vec<i32>) -> i32 {
187+
let n = nums.len();
188+
let (mut ans, mut pre, mut cur) = (0, 0, 0);
189+
190+
for i in 0..n {
191+
cur += 1;
192+
if i == n - 1 || nums[i] >= nums[i + 1] {
193+
ans = ans.max(cur / 2).max(pre.min(cur));
194+
pre = cur;
195+
cur = 0;
196+
}
197+
}
198+
199+
ans
200+
}
201+
}
202+
```
203+
204+
#### JavaScript
205+
206+
```js
207+
/**
208+
* @param {number[]} nums
209+
* @return {number}
210+
*/
211+
var maxIncreasingSubarrays = function (nums) {
212+
let [ans, pre, cur] = [0, 0, 0];
213+
const n = nums.length;
214+
for (let i = 0; i < n; ++i) {
215+
++cur;
216+
if (i === n - 1 || nums[i] >= nums[i + 1]) {
217+
ans = Math.max(ans, cur >> 1, Math.min(pre, cur));
218+
[pre, cur] = [cur, 0];
219+
}
220+
}
221+
return ans;
222+
};
223+
```
224+
174225
<!-- tabs:end -->
175226

176227
<!-- solution:end -->

‎solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README_EN.md‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ tags:
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1
80+
### Solution 1: Single Pass
81+
82+
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.
83+
84+
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.
85+
86+
Finally, we just need to return $\textit{ans}$.
87+
88+
The time complexity is $O(n),ドル where $n$ is the length of the array. The space complexity is $O(1)$.
8189

8290
<!-- tabs:start -->
8391

@@ -169,6 +177,49 @@ function maxIncreasingSubarrays(nums: number[]): number {
169177
}
170178
```
171179

180+
#### Rust
181+
182+
```rust
183+
impl Solution {
184+
pub fn max_increasing_subarrays(nums: Vec<i32>) -> i32 {
185+
let n = nums.len();
186+
let (mut ans, mut pre, mut cur) = (0, 0, 0);
187+
188+
for i in 0..n {
189+
cur += 1;
190+
if i == n - 1 || nums[i] >= nums[i + 1] {
191+
ans = ans.max(cur / 2).max(pre.min(cur));
192+
pre = cur;
193+
cur = 0;
194+
}
195+
}
196+
197+
ans
198+
}
199+
}
200+
```
201+
202+
#### JavaScript
203+
204+
```js
205+
/**
206+
* @param {number[]} nums
207+
* @return {number}
208+
*/
209+
var maxIncreasingSubarrays = function (nums) {
210+
let [ans, pre, cur] = [0, 0, 0];
211+
const n = nums.length;
212+
for (let i = 0; i < n; ++i) {
213+
++cur;
214+
if (i === n - 1 || nums[i] >= nums[i + 1]) {
215+
ans = Math.max(ans, cur >> 1, Math.min(pre, cur));
216+
[pre, cur] = [cur, 0];
217+
}
218+
}
219+
return ans;
220+
};
221+
```
222+
172223
<!-- tabs:end -->
173224

174225
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxIncreasingSubarrays = function (nums) {
6+
let [ans, pre, cur] = [0, 0, 0];
7+
const n = nums.length;
8+
for (let i = 0; i < n; ++i) {
9+
++cur;
10+
if (i === n - 1 || nums[i] >= nums[i + 1]) {
11+
ans = Math.max(ans, cur >> 1, Math.min(pre, cur));
12+
[pre, cur] = [cur, 0];
13+
}
14+
}
15+
return ans;
16+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn max_increasing_subarrays(nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let (mut ans, mut pre, mut cur) = (0, 0, 0);
5+
6+
for i in 0..n {
7+
cur += 1;
8+
if i == n - 1 || nums[i] >= nums[i + 1] {
9+
ans = ans.max(cur / 2).max(pre.min(cur));
10+
pre = cur;
11+
cur = 0;
12+
}
13+
}
14+
15+
ans
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /