Skip to content

Commit 06f104c

Browse files
chore: add LeetCode daily solution
1 parent 7906f6d commit 06f104c

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Adjacent Increasing Subarrays Detection II (Medium)
2+
3+
**Problem ID:** 3350
4+
**Date:** 2025-10-15
5+
**Link:** https://leetcode.com/problems/adjacent-increasing-subarrays-detection-ii/
6+
7+
## Approach
8+
9+
To solve the problem of detecting the maximum value of \( k \) for which there exist two adjacent strictly increasing subarrays of length \( k \) in the given array `nums`, we can follow a structured approach:
10+
11+
### Main Idea:
12+
The key is to identify and store the lengths of strictly increasing segments in the array. Once we have the lengths of these segments, we can check for adjacent segments that meet the criteria of being strictly increasing and adjacent.
13+
14+
### Steps:
15+
1. **Identify Increasing Segments**: Iterate through the array and determine the lengths of all strictly increasing segments. For each element, compare it to the next one. If it is less than the next, increase the current segment length. If it is not, store the length of the current segment (if greater than 1) and reset the length counter.
16+
17+
2. **Store Segment Lengths**: Maintain a list of the lengths of these increasing segments. For example, if the array is `[2,5,7,8,9,2,3,4,3,1]`, the lengths of increasing segments would be `[5, 3]` (for segments `[2,5,7,8,9]` and `[2,3,4]`).
18+
19+
3. **Check for Adjacent Segments**: Once the lengths of increasing segments are identified, check pairs of adjacent segments. For segments at indices \( i \) and \( i+1 \), the maximum \( k \) is determined by the minimum of the two lengths (i.e., \( k = \min(\text{length}[i], \text{length}[i+1]) \)).
20+
21+
4. **Track Maximum \( k \)**: As you check each pair of adjacent segments, keep track of the maximum value of \( k \) found.
22+
23+
### Data Structures:
24+
- A simple list to store the lengths of the increasing segments will suffice.
25+
- Variables to store the current segment length and the maximum \( k \) found.
26+
27+
### Complexity:
28+
- **Time Complexity**: \( O(n) \) where \( n \) is the length of the input array. This is because we make a single pass through the array to identify increasing segments and another pass through the segment lengths to find adjacent pairs.
29+
- **Space Complexity**: \( O(m) \) where \( m \) is the number of increasing segments identified. In the worst case, this could be \( O(n) \), but typically it will be much smaller as segments of increasing lengths are often merged.
30+
31+
By following this structured approach, we can efficiently determine the maximum \( k \) for which two adjacent strictly increasing subarrays exist in the given array.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int maxLengthBetweenIncreasingSubarrays(int[] nums) {
3+
int n = nums.length;
4+
int maxK = 0;
5+
6+
// Array to store lengths of increasing subarrays
7+
int[] lengths = new int[n];
8+
9+
// Calculate lengths of strictly increasing subarrays
10+
for (int i = 1; i < n; i++) {
11+
if (nums[i] > nums[i - 1]) {
12+
lengths[i] = lengths[i - 1] + 1;
13+
}
14+
}
15+
16+
// Find the maximum k for adjacent increasing subarrays
17+
for (int i = 1; i < n; i++) {
18+
if (lengths[i] > 0 && lengths[i - 1] > 0) {
19+
maxK = Math.max(maxK, Math.min(lengths[i], lengths[i - 1]) + 1);
20+
}
21+
}
22+
23+
return maxK;
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var maxK = function(nums) {
2+
const n = nums.length;
3+
let maxK = 0;
4+
const lengths = new Array(n).fill(0);
5+
6+
// Calculate lengths of strictly increasing subarrays
7+
for (let i = 1; i < n; i++) {
8+
if (nums[i] > nums[i - 1]) {
9+
lengths[i] = lengths[i - 1] + 1;
10+
}
11+
}
12+
13+
// Find the maximum k for adjacent strictly increasing subarrays
14+
for (let i = 1; i < n; i++) {
15+
if (lengths[i] > 0 && lengths[i - 1] > 0) {
16+
const k = Math.min(lengths[i], lengths[i - 1]) + 1;
17+
maxK = Math.max(maxK, k);
18+
}
19+
}
20+
21+
return maxK;
22+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def maxK(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
if n < 2:
5+
return 0
6+
7+
lengths = [0] * n
8+
9+
for i in range(1, n):
10+
if nums[i] > nums[i - 1]:
11+
lengths[i] = lengths[i - 1] + 1
12+
13+
max_k = 0
14+
15+
for i in range(1, n):
16+
if lengths[i] > 0 and lengths[i - 1] > 0:
17+
k = min(lengths[i], lengths[i - 1]) + 1
18+
max_k = max(max_k, k)
19+
20+
return max_k

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
248248
- 2025-10-12 — [Find Sum of Array Product of Magical Sequences](https://leetcode.com/problems/find-sum-of-array-product-of-magical-sequences/) (Hard) → `Hard/2025-10-12-3539-Find-Sum-of-Array-Product-of-Magical-Sequences`
249249
- 2025-10-13 — [Find Resultant Array After Removing Anagrams](https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/) (Easy) → `Easy/2025-10-13-2273-Find-Resultant-Array-After-Removing-Anagrams`
250250
- 2025-10-14 — [Adjacent Increasing Subarrays Detection I](https://leetcode.com/problems/adjacent-increasing-subarrays-detection-i/) (Easy) → `Easy/2025-10-14-3349-Adjacent-Increasing-Subarrays-Detection-I`
251+
- 2025-10-15 — [Adjacent Increasing Subarrays Detection II](https://leetcode.com/problems/adjacent-increasing-subarrays-detection-ii/) (Medium) → `Medium/2025-10-15-3350-Adjacent-Increasing-Subarrays-Detection-II`

0 commit comments

Comments
 (0)