Count Strictly Increasing Subarrays — LeetCode 2393 Python Solution

MediumLeetCode PremiumArrayMathDynamic Programming
Problem
#2393
Reading time
2 min

The problem

You are given an array nums consisting of positive integers. Return the number of subarrays of nums that are in strictly increasing order.

Example

Input
nums = [1,3,5,4,4,6]
Output
10
Explanation
The strictly increasing subarrays are the following:

Python solution

Python
class Solution:
    def countSubarrays(self, nums: List[int]) -> int:
        ans = cnt = 1
        for x, y in pairwise(nums):
            if x < y:
                cnt += 1
            else:
                cnt = 1
            ans += cnt
        return ans

Complexity

MeasureComplexity
TimeO(n), where n is the length of the array
SpaceO(1) auxiliary

Pattern: Dynamic Programming

Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 2393. Count Strictly Increasing Subarrays is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.

The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2393. Count Strictly Increasing Subarrays?
LeetCode 2393. Count Strictly Increasing Subarrays is rated Medium on LeetCode.
What is the time complexity of LeetCode 2393. Count Strictly Increasing Subarrays?
The Python solution on this page runs in O(n), where n is the length of the array.
What is the space complexity of LeetCode 2393. Count Strictly Increasing Subarrays?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2393. Count Strictly Increasing Subarrays cover?
LeetCode 2393. Count Strictly Increasing Subarrays is tagged Array, Math and Dynamic Programming on LeetCode.
Is LeetCode 2393. Count Strictly Increasing Subarrays a premium problem?
Yes. LeetCode 2393. Count Strictly Increasing Subarrays is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview