Skip to content

2393. Count Strictly Increasing Subarrays 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  long long countSubarrays(vector<int>& nums) {
    long long ans = 0;

    for (int i = 0, j = -1; i < nums.size(); ++i) {
      if (i > 0 && nums[i] <= nums[i - 1])
        j = i - 1;
      ans += i - j;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public long countSubarrays(int[] nums) {
    long ans = 0;

    for (int i = 0, j = -1; i < nums.length; ++i) {
      if (i > 0 && nums[i] <= nums[i - 1])
        j = i - 1;
      ans += i - j;
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
  def countSubarrays(self, nums: list[int]) -> int:
    ans = 0

    j = -1
    for i, num in enumerate(nums):
      if i > 0 and num <= nums[i - 1]:
        j = i - 1
      ans += i - j

    return ans