3349. Adjacent Increasing Subarrays Detection I
UnknownView on LeetCode
Time: O(n)
Space: O(n)
Advertisement
Approach
Compute run lengths of strictly increasing subarrays; check adjacent runs ≥ k.
Key Techniques
Array
Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.
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?