Skip to content

Commit 8a04729

Browse files
committed
chore: update 3350. Adjacent Increasing Subarrays Detection II
1 parent b2fe219 commit 8a04729

4 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: 3350. Adjacent Increasing Subarrays Detection II
3+
description: Given an array nums of n integers, your task is to find the maximum value of k for which there exist two adjacent subarrays of length k each, such that both subarrays are strictly increasing. Specifically, check if there are two subarrays of length k starting at indices a and b (a < b)
4+
keywords:
5+
[
6+
LeetCode,
7+
3350. Adjacent Increasing Subarrays Detection II,
8+
Medium,
9+
Array,
10+
Binary Search,
11+
]
12+
tags: [LeetCode, Medium, Array, Binary Search]
13+
---
14+
15+
# [{frontMatter.title}](https://leetcode.com/problems/adjacent-increasing-subarrays-detection-ii)
16+
17+
import Tabs from "@theme/Tabs";
18+
import TabItem from "@theme/TabItem";
19+
import CodeBlock from "@theme/CodeBlock";
20+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
21+
import Solution from "!!raw-loader!./solution.js";
22+
import Description from "./_Description.md";
23+
import Examples from "./_Examples.md";
24+
25+
<DifficultyBadge difficulty="Medium" />
26+
27+
<Tabs>
28+
<TabItem value="description" label="題目描述" default>
29+
<details open>
30+
<summary>Description</summary>
31+
<Description />
32+
<Examples />
33+
</details>
34+
</TabItem>
35+
36+
<TabItem value="solution" label="解答">
37+
## Solution
38+
<CodeBlock language="js">{Solution}</CodeBlock>
39+
</TabItem>
40+
</Tabs>
41+
42+
### 解題思路
43+
44+
[上一題](../3349.%20Adjacent%20Increasing%20Subarrays%20Detection%20I/README.mdx) 不同的是這次題目沒有給 `k`,而是變成題目要找出最大的 `k`,且兩個相鄰的 subarray 也都要是 strictly increasing。
45+
46+
1. 首先由於測試範圍是 `2 <= nums.length <= 2 * 105`,故在最小的 nums.length = 2 時直接 return 0。
47+
以及宣告變數
48+
- `count` 來記錄當前的 `k`
49+
- `preCount` 來紀錄前一段的 `k`
50+
- `result` 來紀錄最終的答案 (最大的 `k` )。
51+
52+
```js
53+
if (nums.length <= 1) {
54+
return 0;
55+
}
56+
57+
let count = 1;
58+
let preCount = 0;
59+
let result = 1;
60+
```
61+
62+
2. 接著遍歷陣列,如果下一個數字大於現在的數字,代表目前 strictly increasing,將 `count` 加一,否則就更新 `result`
63+
64+
```js
65+
for (let i = 1; i < nums.length; i++) {
66+
if (nums[i] > nums[i - 1]) {
67+
count++;
68+
} else {
69+
result = Math.max(result, Math.floor(count / 2));
70+
result = Math.max(result, Math.min(count, preCount));
71+
preCount = count; // 將 preCount 更新為當前的 count
72+
count = 1; // 重置 count
73+
}
74+
}
75+
```
76+
77+
<details open>
78+
<summary>更新 result 的步驟</summary>
79+
<Tabs>
80+
<TabItem value="Math.floor(count / 2)" label="Math.floor(count / 2)">
81+
82+
> `Math.floor(count / 2)` 是用來處理「單一區段連續遞增」的情況。
83+
84+
假設 `nums = [1, 2, 3, 4, 5, 6]`,這裡的 count = 6。
85+
且因為這一整段是遞增的,所以可以被切成兩個長度為 3 的子陣列:
86+
87+
- `[1, 2, 3]`
88+
- `[4, 5, 6]`
89+
90+
所以這段裡面最大的 k 可以是 count / 2 = 3。(如果長度是奇數,比如 5,那最大可切成 Math.floor(5 / 2) = 2。)
91+
92+
</TabItem>
93+
<TabItem value="Math.min(count, preCount)" label="Math.min(count, preCount)">
94+
95+
> `Math.min(count, preCount)` 是用來處理「有斷掉」的情況。
96+
97+
假設 `nums = [2, 3, 4, 5, 1, 2, 3]`,這裡的 count = 7。
98+
99+
但這整段不是 strictly increasing,中間有斷掉,故可以拆成
100+
- `[2, 3, 4, 5]` // count = 4
101+
- `[1, 2, 3]` // count = 3,preCount = 4
102+
103+
而形成的最大 `k` 是兩段中「較短」的那一段長度,故上述例子的最大 `k` 為 3。
104+
105+
</TabItem>
106+
107+
</Tabs>
108+
</details>
109+
110+
3. 而 for 迴圈結束後最後一段遞增區間還沒被結算
111+
> 假設 `nums = [1, 2, 3, 4, 5]`,迴圈跑完時,count = 5,但因為從頭到尾沒遇到遞增中斷,else 從未執行
112+
113+
因此最後再判斷一次即可。
114+
115+
```js
116+
result = Math.max(result, Math.floor(count / 2));
117+
result = Math.max(result, Math.min(count, preCount));
118+
119+
return result;
120+
```
121+
122+
### 心得
123+
124+
複雜死ㄌ:(
125+
一直鬼打牆瘋狂看解析才搞懂原理。
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Given an array nums of n integers, your task is to find the maximum value of k for which there exist two adjacent subarrays of length k each, such that both subarrays are strictly increasing. Specifically, check if there are two subarrays of length k starting at indices a and b (a < b), where:
2+
3+
- Both subarrays nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing.
4+
- The subarrays must be adjacent, meaning b = a + k.
5+
6+
Return the maximum possible value of k.
7+
8+
A subarray is a contiguous non-empty sequence of elements within an array.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#### Example 1:
2+
3+
> **Input:** nums = [2,5,7,8,9,2,3,4,3,1]
4+
> **Output:** 3
5+
> **Explanation:**
6+
>
7+
> - The subarray starting at index 2 is `[7, 8, 9]`, which is strictly increasing.
8+
> - The subarray starting at index 5 is `[2, 3, 4]`, which is also strictly increasing.
9+
> - These two subarrays are adjacent, and 3 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist.
10+
11+
#### Example 2:
12+
13+
> **Input:** nums = [1,2,3,4,4,4,4,5,6,7]
14+
> **Output:** 2
15+
> **Explanation:**
16+
>
17+
> - The subarray starting at index 0 is `[1, 2]`, which is strictly increasing.
18+
> - The subarray starting at index 2 is `[3, 4]`, which is also strictly increasing.
19+
> - These two subarrays are adjacent, and 2 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist.
20+
21+
#### Constraints:
22+
23+
- `2 <= nums.length <= 2 * 105`
24+
- `-109 <= nums[i] <= 109`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxIncreasingSubarrays = function (nums) {
6+
if (nums.length <= 1) {
7+
return 0;
8+
}
9+
10+
let count = 1;
11+
let preCount = 0;
12+
let result = 1;
13+
14+
for (let i = 1; i < nums.length; i++) {
15+
if (nums[i] > nums[i - 1]) {
16+
count++;
17+
} else {
18+
result = Math.max(result, Math.floor(count / 2));
19+
result = Math.max(result, Math.min(count, preCount));
20+
preCount = count;
21+
count = 1;
22+
}
23+
}
24+
25+
result = Math.max(result, Math.floor(count / 2));
26+
result = Math.max(result, Math.min(count, preCount));
27+
28+
return result;
29+
};

0 commit comments

Comments
 (0)