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 <= 10^5
1 <= nums[i], minK, maxK <= 10^6
Solve this problem and provide a detailed explanation of your solution, including time and space complexity analysis. Discuss potential edge cases and how your solution handles them. Provide example code with comments.
def count_fixed_bound_subarrays(nums, minK, maxK):
n = len(nums)
count = 0
for i in range(n):
for j in range(i, n):
subarray = nums[i:j+1]
if minK in subarray and maxK in subarray and min(subarray) == minK and max(subarray) == maxK:
count += 1
return count
# Example usage:
nums1 = [1, 3, 5, 2, 7, 5]
minK1 = 1
maxK1 = 5
print(f"Example 1: {count_fixed_bound_subarrays(nums1, minK1, maxK1)}") # Output: 2
nums2 = [1, 1, 1, 1]
minK2 = 1
maxK2 = 1
print(f"Example 2: {count_fixed_bound_subarrays(nums2, minK2, maxK2)}") # Output: 10
nums3 = [1, 3, 5, 2]
minK3 = 2
maxK3 = 5
print(f"Example 3: {count_fixed_bound_subarrays(nums3, minK3, maxK3)}") # Output: 1
nums4 = [1, 5, 2, 3, 4]
minK4 = 4
maxK4 = 4
print(f"Example 4: {count_fixed_bound_subarrays(nums4, minK4, maxK4)}") # Output: 0
def count_fixed_bound_subarrays_optimal(nums, minK, maxK):
n = len(nums)
count = 0
left = -1 # Leftmost index of invalid element
min_idx = -1 # Last seen index of minK
max_idx = -1 # Last seen index of maxK
for i in range(n):
if nums[i] < minK or nums[i] > maxK:
left = i
if nums[i] == minK:
min_idx = i
if nums[i] == maxK:
max_idx = i
if min_idx != -1 and max_idx != -1:
start = max(left, min(min_idx, max_idx))
end = min(min_idx, max_idx)
count += end - left
if left >= end: #no possible subarrays.
count = count - (end-left)
return count
# Example usage:
nums1 = [1, 3, 5, 2, 7, 5]
minK1 = 1
maxK1 = 5
print(f"Example 1: {count_fixed_bound_subarrays_optimal(nums1, minK1, maxK1)}") # Output: 2
nums2 = [1, 1, 1, 1]
minK2 = 1
maxK2 = 1
print(f"Example 2: {count_fixed_bound_subarrays_optimal(nums2, minK2, maxK2)}") # Output: 10
nums3 = [1, 3, 5, 2]
minK3 = 2
maxK3 = 5
print(f"Example 3: {count_fixed_bound_subarrays_optimal(nums3, minK3, maxK3)}") # Output: 1
nums4 = [1, 5, 2, 3, 4]
minK4 = 4
maxK4 = 4
print(f"Example 4: {count_fixed_bound_subarrays_optimal(nums4, minK4, maxK4)}") # Output: 0
nums5 = [6, 3, 4, 5, 6, 5, 6, 4, 3]
minK5 = 3
maxK5 = 5
print(f"Example 5: {count_fixed_bound_subarrays_optimal(nums5, minK5, maxK5)}") # Output: 6
nums6 = [4,3,8,6,4,1,8,8]
minK6 = 1
maxK6 = 8
print(f"Example 6: {count_fixed_bound_subarrays_optimal(nums6, minK6, maxK6)}") # Output 2
nums7 = [7,1,8,4,9,5,7,9,7,8,5,1,4,9,8]
minK7 = 1
maxK7 = 9
print(f"Example 7: {count_fixed_bound_subarrays_optimal(nums7, minK7, maxK7)}") # Output 6
Naive Solution:
n
times.n
times.min
and max
functions on a subarray take O(k) where k is the length of the subarray.Optimal Solution:
Naive Solution:
Optimal Solution:
minK
and maxK
are equal. In this case, the subarray must only contain the value minK
.minK
and maxK
in the array, the algorithm should still work correctly.minK
or maxK
is not in the array.nums = [5]
and minK = 5
, maxK = 5
, the answer should be 1.minK
and maxK
, but the count should accurately reflect the number of valid subarrays.