Count Strictly Increasing Subarrays — LeetCode 2393 Python Solution
- Problem
- #2393
- Pattern
- Dynamic Programming
- Reading time
- 2 min
- Source
- leetcode.com
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
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 ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array |
| Space | O(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.