philosyang.com

3350. Adjacent Increasing Subarrays Detection II | 1600

Happy halloween?

 1class Solution:
 2    def maxIncreasingSubarrays(self, nums: List[int]) -> int:
 3        # strictly increasing combo array
 4        # [2,5,7,8,9,2,3,4,3,1]
 5        # [5 4 3 2 1 3 2 1 1 1] expand from back to front when back > front
 6        # [5 x x x x y x x x x] we need to check (if y==5) n times, each costing O(1) idx value lookup
 7        # early skip includes if lower than current best result
 8
 9        # also possible:
10        # [5 4 3 2 1 3 2 1 1 1] expand from back to front when back > front
11        # [x x y x x 3 x x y x] we need to check (if y>=3) n times, each costing O(2) idx value lookup
12
13        # also possible:
14        # [1,2,3,4,5,6,7,8,0,0]
15        # [8 7 6 5 4 3 2 1 1 1] expand from back to front when back > front
16        # [x x a x x b x x c x]
17        # when at b, we check whether a >= b or c >= b
18
19        n = len(nums)
20        combo = [1] * n
21        k = 1
22
23        for i in range(n - 2, -1, -1):
24            combo[i] = combo[i + 1] + 1 if nums[i + 1] > nums[i] else 1
25
26        # print(combo)
27
28        for i in range(n):
29            current_combo = combo[i]
30            if current_combo > k:
31                prior_idx = i - combo[i]
32                post_idx = i + combo[i]
33                if prior_idx >= 0 and current_combo <= combo[prior_idx]:
34                    k = max(k, current_combo)
35                elif post_idx < n and current_combo <= combo[post_idx]:
36                    k = max(k, current_combo)
37
38        return k

#Array #Python