diff options
Diffstat (limited to 'count_subarr_fixed_bound.py')
-rw-r--r-- | count_subarr_fixed_bound.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/count_subarr_fixed_bound.py b/count_subarr_fixed_bound.py new file mode 100644 index 0000000..44fd99c --- /dev/null +++ b/count_subarr_fixed_bound.py @@ -0,0 +1,35 @@ +# 2444. Count Subarrays With Fixed Bounds + +""" +given an integer array 'nums' and two integer 'mink' and 'maxk'. a fixed +bound subarray of 'nums' is a subarray that satisfies the following +conditions. the minimum value in the subarray is equal to 'mink' and the +maximum value in the subarray is equal to 'maxk'. return the number of fixed +bound subarrays. a subarray is a contiguous part of an array. +""" + + +class Solution(object): + def countSubarrays(self, nums, minK, maxK): + """ + :type nums: List[int] + :type minK: int + :type maxK: int + :rtype: int + """ + ans, last_min, last_max, bad = 0, -1, -1, -1 + for i, a in enumerate(nums): + if not minK <= a <= maxK: + bad = i + if a == minK: + last_min = i + if a == maxK: + last_max = i + ans += max(0, min(last_min, last_max) - bad) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.countSubarrays(nums=[1, 3, 5, 2, 7, 5], minK=1, maxK=5)) # expect: 2 + print(obj.countSubarrays(nums=[1, 1, 1, 1], minK=1, maxK=1)) # expect: 10 |