Skip to main content
impol.dev

Editorial

Solution walkthrough

Review the explanation, then compare the reference implementation.

Video explanation

Solution

Intuition

Fix the boundary between the two adjacent subarrays. The left block may extend through the increasing run ending at that boundary, and the right block through the increasing run starting just after it. Their common length is limited by the shorter run.

Approach

Scan left to right to compute end_len[i], the length of the strictly increasing run ending at i. Extend the previous length only when nums[i] > nums[i - 1]; otherwise retain one. Scan right to left similarly to compute start_len[i], the increasing run starting at i.

For every boundary between i and i + 1, consider min(end_len[i], start_len[i + 1]) and return the largest candidate. Taking that many elements on each side gives valid adjacent blocks, and no longer pair can use the same boundary. No comparison across the boundary is required, even when both blocks lie inside one longer increasing run.

class Solution: def maxIncreasingSubarrays(self, nums: List[int]) -> int: n = len(nums) end_len = [1] * n for i in range(1, n): if nums[i] > nums[i - 1]: end_len[i] = end_len[i - 1] + 1 start_len = [1] * n for i in range(n - 2, -1, -1): if nums[i] < nums[i + 1]: start_len[i] = start_len[i + 1] + 1 res = 1 for i in range(n - 1): res = max(res, min(end_len[i], start_len[i + 1])) return res

Complexity analysis

Let n = nums.length. Time: O(n). Auxiliary space: O(n) for increasing-run lengths ending and starting at each index.