-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2444-count-subarrays-with-fixed-bounds.js
More file actions
39 lines (36 loc) · 1.04 KB
/
2444-count-subarrays-with-fixed-bounds.js
File metadata and controls
39 lines (36 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 2444. Count Subarrays With Fixed Bounds
* https://leetcode.com/problems/count-subarrays-with-fixed-bounds/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {number[]} nums
* @param {number} minK
* @param {number} maxK
* @return {number}
*/
var countSubarrays = function(nums, minK, maxK) {
let result = 0;
for (let i = 0, minIndex = -1, maxIndex = -1, invalidIndex = -1; i < nums.length; i++) {
if (nums[i] < minK || nums[i] > maxK) {
invalidIndex = i;
}
if (nums[i] === minK) {
minIndex = i;
}
if (nums[i] === maxK) {
maxIndex = i;
}
result += Math.max(0, Math.min(minIndex, maxIndex) - invalidIndex);
}
return result;
};