Skip to content

Commit 8a4561b

Browse files
committed
Add solution #2444
1 parent b737104 commit 8a4561b

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,465 LeetCode solutions in JavaScript
1+
# 1,466 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1372,6 +1372,7 @@
13721372
2425|[Bitwise XOR of All Pairings](./solutions/2425-bitwise-xor-of-all-pairings.js)|Medium|
13731373
2427|[Number of Common Factors](./solutions/2427-number-of-common-factors.js)|Easy|
13741374
2429|[Minimize XOR](./solutions/2429-minimize-xor.js)|Medium|
1375+
2444|[Count Subarrays With Fixed Bounds](./solutions/2444-count-subarrays-with-fixed-bounds.js)|Hard|
13751376
2460|[Apply Operations to an Array](./solutions/2460-apply-operations-to-an-array.js)|Easy|
13761377
2462|[Total Cost to Hire K Workers](./solutions/2462-total-cost-to-hire-k-workers.js)|Medium|
13771378
2467|[Most Profitable Path in a Tree](./solutions/2467-most-profitable-path-in-a-tree.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 2444. Count Subarrays With Fixed Bounds
3+
* https://leetcode.com/problems/count-subarrays-with-fixed-bounds/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array nums and two integers minK and maxK.
7+
*
8+
* A fixed-bound subarray of nums is a subarray that satisfies the following conditions:
9+
* - The minimum value in the subarray is equal to minK.
10+
* - The maximum value in the subarray is equal to maxK.
11+
* - Return the number of fixed-bound subarrays.
12+
*
13+
* A subarray is a contiguous part of an array.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @param {number} minK
19+
* @param {number} maxK
20+
* @return {number}
21+
*/
22+
var countSubarrays = function(nums, minK, maxK) {
23+
let result = 0;
24+
25+
for (let i = 0, minIndex = -1, maxIndex = -1, invalidIndex = -1; i < nums.length; i++) {
26+
if (nums[i] < minK || nums[i] > maxK) {
27+
invalidIndex = i;
28+
}
29+
if (nums[i] === minK) {
30+
minIndex = i;
31+
}
32+
if (nums[i] === maxK) {
33+
maxIndex = i;
34+
}
35+
result += Math.max(0, Math.min(minIndex, maxIndex) - invalidIndex);
36+
}
37+
38+
return result;
39+
};

0 commit comments

Comments
 (0)