You are given an integer array nums and two integers minK and maxK.
A fixed-bound subarray of nums is a subarray that satisfies the following conditions:
minK.maxK.Return the number of fixed-bound subarrays.
A subarray is a contiguous part of an array.
Example 1:
Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5 Output: 2 Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
Example 2:
Input: nums = [1,1,1,1], minK = 1, maxK = 1 Output: 10 Explanation: Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
Constraints:
2 <= nums.length <= 1051 <= nums[i], minK, maxK <= 106class Solution {
public long countSubarrays(int[] nums, int minK, int maxK) {
long count = 0;
int start = -1, mini = -1, maxi = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < minK || nums[i] > maxK) start = i;
if (nums[i] == maxK) maxi = i;
if (nums[i] == minK) mini = i;
int valid = Math.max(0, Math.min(mini, maxi) - start);
count += valid;
}
return count;
}
}