Skip to content

Latest commit

 

History

History
25 lines (23 loc) · 622 Bytes

795.md

File metadata and controls

25 lines (23 loc) · 622 Bytes

795. Number of Subarrays with Bounded Maximum

Solution 1 (time O(n), space O(1))

class Solution(object):
    def numSubarrayBoundedMax(self, nums, left, right):
        """
        :type nums: List[int]
        :type left: int
        :type right: int
        :rtype: int
        """
        ans = 0
        p1 = p2 = -1
        for i in range(len(nums)):
            if left <= nums[i] <= right:
                p1 = i
            elif nums[i] > right:
                p1 = -1
                p2 = i
            if p1 != -1:
                ans += p1 - p2
        return ans