Skip to content

Commit 35e3bff

Browse files
authored
Create 3350-adjacent-increasing-subarrays-detection-ii.js
1 parent 154527e commit 35e3bff

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxIncreasingSubarrays = function (nums) {
6+
const n = nums.length
7+
8+
const increasingRun = new Array(n).fill(1)
9+
for (let i = n - 2; i >= 0; --i) {
10+
if (nums[i] < nums[i + 1]) {
11+
increasingRun[i] = increasingRun[i + 1] + 1
12+
}
13+
}
14+
15+
let left = 1,
16+
right = Math.floor(n / 2)
17+
let res = 0
18+
19+
while (left <= right) {
20+
const mid = left + Math.floor((right - left) / 2)
21+
let found = false
22+
23+
for (let i = 0; i <= n - 2 * mid; ++i) {
24+
if (increasingRun[i] >= mid && increasingRun[i + mid] >= mid) {
25+
found = true
26+
break
27+
}
28+
}
29+
30+
if (found) {
31+
res = mid
32+
left = mid + 1
33+
} else {
34+
right = mid - 1
35+
}
36+
}
37+
38+
return res
39+
}

0 commit comments

Comments
 (0)