[LeetCode] Count Strictly Increasing Subarrays

2393. Count Strictly Increasing Subarrays

You are given an array nums consisting of positive integers.

Return the number of subarrays of nums that are in strictly increasing order.

A subarray is a contiguous part of an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
long long countSubarrays(vector<int>& nums) {
long long res = 0, now = 1;
nums.push_back(-1);
for(int i = 1; i < nums.size(); i++) {
if(nums[i] > nums[i-1]) now += 1;
else {
res += now * (now + 1) / 2;
now = 1;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/02/01/PS/LeetCode/count-strictly-increasing-subarrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.