|
| 1 | +/** |
| 2 | + * 3350. Adjacent Increasing Subarrays Detection II |
| 3 | + * https://leetcode.com/problems/adjacent-increasing-subarrays-detection-ii/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * Given an array nums of n integers, your task is to find the maximum value of k for which |
| 7 | + * there exist two adjacent subarrays of length k each, such that both subarrays are strictly |
| 8 | + * increasing. Specifically, check if there are two subarrays of length k starting at indices |
| 9 | + * a and b (a < b), where: |
| 10 | + * - Both subarrays nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing. |
| 11 | + * - The subarrays must be adjacent, meaning b = a + k. |
| 12 | + * |
| 13 | + * Return the maximum possible value of k. |
| 14 | + * |
| 15 | + * A subarray is a contiguous non-empty sequence of elements within an array. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {number[]} nums |
| 20 | + * @return {number} |
| 21 | + */ |
| 22 | +var maxIncreasingSubarrays = function(nums) { |
| 23 | + const n = nums.length; |
| 24 | + const lengths = new Array(n).fill(1); |
| 25 | + |
| 26 | + for (let i = n - 2; i >= 0; i--) { |
| 27 | + if (nums[i] < nums[i + 1]) { |
| 28 | + lengths[i] = lengths[i + 1] + 1; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + let result = 0; |
| 33 | + for (let i = 0; i < n; i++) { |
| 34 | + const currentLength = lengths[i]; |
| 35 | + result = Math.max(result, Math.floor(currentLength / 2)); |
| 36 | + |
| 37 | + const nextIndex = i + currentLength; |
| 38 | + if (nextIndex < n) { |
| 39 | + const minLength = Math.min(currentLength, lengths[nextIndex]); |
| 40 | + result = Math.max(result, minLength); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return result; |
| 45 | +}; |
0 commit comments