Advertisement
3349. Adjacent Increasing Subarrays Detection I
UnknownView on LeetCode
Time: O(n)
Space: O(n)
Approach
Compute run lengths of strictly increasing subarrays; check adjacent runs ≥ k.
3349.cs
C#
// Approach: Compute run lengths of strictly increasing subarrays; check adjacent runs ≥ k.
// Time: O(n) Space: O(n)
public class Solution
{
public bool HasIncreasingSubarrays(IList<int> nums, int k)
{
int maxValidLength = 0;
int previousLength = 0;
int currentLength = 0;
int n = nums.Count;
for (int i = 0; i < n; i++)
{
currentLength++;
if (i == n - 1 || nums[i] >= nums[i + 1])
{
maxValidLength = Math.Max(maxValidLength,
Math.Max(currentLength / 2,
Math.Min(previousLength, currentLength)));
previousLength = currentLength;
currentLength = 0;
}
}
return maxValidLength >= k;
}
}Advertisement
Was this solution helpful?