2444. Count Subarrays With Fixed Bounds LeetCode Solution
In this guide, you will get 2444. Count Subarrays With Fixed Bounds LeetCode Solution with the best time and space complexity. The solution to Count Subarrays With Fixed Bounds problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Subarrays With Fixed Bounds solution in C++
- Count Subarrays With Fixed Bounds solution in Java
- Count Subarrays With Fixed Bounds solution in Python
- Additional Resources
Problem Statement of Count Subarrays With Fixed Bounds
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:
The minimum value in the subarray is equal to minK.
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.
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 <= 105
1 <= nums[i], minK, maxK <= 106
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2444. Count Subarrays With Fixed Bounds LeetCode Solution in C++
class Solution {
public:
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
long ans = 0;
int j = -1;
int prevMinKIndex = -1;
int prevMaxKIndex = -1;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] < minK || nums[i] > maxK)
j = i;
if (nums[i] == minK)
prevMinKIndex = i;
if (nums[i] == maxK)
prevMaxKIndex = i;
// Any index k in [j + 1, min(prevMinKIndex, prevMaxKIndex)] can be the
// start of the subarray s.t. nums[k..i] satisfies the conditions.
ans += max(0, min(prevMinKIndex, prevMaxKIndex) - j);
}
return ans;
}
};
/* code provided by PROGIEZ */
2444. Count Subarrays With Fixed Bounds LeetCode Solution in Java
class Solution {
public long countSubarrays(int[] nums, int minK, int maxK) {
long ans = 0;
int j = -1;
int prevMinKIndex = -1;
int prevMaxKIndex = -1;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] < minK || nums[i] > maxK)
j = i;
if (nums[i] == minK)
prevMinKIndex = i;
if (nums[i] == maxK)
prevMaxKIndex = i;
// Any index k in [j + 1, min(prevMinKIndex, prevMaxKIndex)] can be the
// start of the subarray s.t. nums[k..i] satisfies the conditions.
ans += Math.max(0, Math.min(prevMinKIndex, prevMaxKIndex) - j);
}
return ans;
}
}
// code provided by PROGIEZ
2444. Count Subarrays With Fixed Bounds LeetCode Solution in Python
class Solution:
def countSubarrays(self, nums: list[int], minK: int, maxK: int) -> int:
ans = 0
j = -1
prevMinKIndex = -1
prevMaxKIndex = -1
for i, num in enumerate(nums):
if num < minK or num > maxK:
j = i
if num == minK:
prevMinKIndex = i
if num == maxK:
prevMaxKIndex = i
# Any index k in [j + 1, min(prevMinKIndex, prevMaxKIndex)] can be the
# start of the subarray s.t. nums[k..i] satisfies the conditions.
ans += max(0, min(prevMinKIndex, prevMaxKIndex) - j)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.