Editorial
Solution walkthrough
Review the explanation, then compare the reference implementation.
Video explanation
Solution
Intuition
For a proposed starting index, test whether the next k elements increase strictly and whether the immediately following k elements do too. The boundary between the two blocks does not need to increase. The small constraints allow retrying successive starts.
Approach
From the current position, scan increasing adjacent values until either a comparison fails, the array ends, or k elements have been collected. Use a flag to remember whether one full block has just succeeded.
After the first successful block, continue exactly at the following position to test its adjacent partner. A second success returns true. If either block fails, clear the flag, advance the original candidate start by one, and retry from there. Return false if the search ends without two successes.
The scan checks only comparisons inside each block, so it correctly permits a decrease or equality between them. For k = 1, every single-element block is increasing, and two adjacent positions suffice.
class Solution:
def hasIncreasingSubarrays(self, nums: List[int], k: int) -> bool:
n = len(nums)
one = False
last = 0
i = 0
while i < n:
count = 1
j = i + 1
while j < n and nums[j] > nums[j - 1] and count < k:
j += 1
count += 1
if count == k:
if one:
return True
one = True
i = j
else:
one = False
last += 1
i = last
return FalseComplexity analysis
Let n = nums.length. Time: O(n * k) in the worst case because a length-k increasing run can be rescanned from successive starts. Auxiliary space: O(1).
