Skip to content

Commit b829503

Browse files
committed
feat(arrays, subarrays): count subarrays with fixed bounds
1 parent eb9734c commit b829503

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Count Subarrays with Fixed Bounds
2+
3+
Given an integer array, nums, and two integers minK and maxK, return the number of fixed-bound subarrays.
4+
5+
A subarray in nums is called a fixed-bound subarray if it satisfies the following conditions:
6+
7+
1. The smallest value in the subarray equals `minK`.
8+
2. The largest value in the subarray equals `maxK`.
9+
10+
> Note: A subarray is a contiguous sequence of elements within an array.
11+
12+
Constraints:
13+
14+
- 2 ≤ nums.length ≤ 10^3
15+
- 1 ≤ nums[i], minK, maxK ≤10^3
16+
17+
## Examples
18+
19+
![Example One](./images/subarrays_with_fixed_bounds_example_one.png)
20+
![Example Two](./images/subarrays_with_fixed_bounds_example_two.png)
21+
![Example Three](./images/subarrays_with_fixed_bounds_example_three.png)
22+
![Example Four](./images/subarrays_with_fixed_bounds_example_four.png)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
def count_subarrays(nums: List[int], min_k: int, max_k: int) -> int:
5+
"""
6+
Counts the number of subarrays in the provided array which satisfies the following conditions:
7+
1. The smallest value in the subarray equals min_k.
8+
2. The largest value in the subarray equals max_k.
9+
10+
Args:
11+
nums (List[int]): The array of integers.
12+
min_k (int): The minimum value in the subarray.
13+
max_k (int): The maximum value in the subarray.
14+
15+
Returns:
16+
int: The number of subarrays that satisfies the conditions.
17+
"""
18+
last_min, last_max, last_invalid = -1, -1, -1
19+
count = 0
20+
21+
for i, num in enumerate(nums):
22+
if num == min_k:
23+
last_min = i
24+
if num == max_k:
25+
last_max = i
26+
if num < min_k or num > max_k:
27+
last_invalid = i
28+
29+
count += max(0, min(last_min, last_max) - last_invalid)
30+
31+
return count
24.1 KB
Loading
29.8 KB
Loading
31.5 KB
Loading
27.9 KB
Loading
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import unittest
2+
from . import count_subarrays
3+
4+
5+
class SubarraysWithFixedBoundsTestCase(unittest.TestCase):
6+
def test_1(self):
7+
nums = [2,1,4,3,2]
8+
min_k = 2
9+
max_k = 3
10+
expected = 1
11+
actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k)
12+
self.assertEqual(expected, actual)
13+
14+
def test_2(self):
15+
nums = [1,2,3,2,1]
16+
min_k = 1
17+
max_k = 3
18+
expected = 5
19+
actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k)
20+
self.assertEqual(expected, actual)
21+
22+
def test_3(self):
23+
nums = [4,4,4]
24+
min_k = 4
25+
max_k = 4
26+
expected = 6
27+
actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k)
28+
self.assertEqual(expected, actual)
29+
30+
def test_4(self):
31+
nums = [2,2,2]
32+
min_k = 4
33+
max_k = 4
34+
expected = 0
35+
actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k)
36+
self.assertEqual(expected, actual)
37+
38+
def test_5(self):
39+
nums = [1,3,5,2,7,5]
40+
min_k = 1
41+
max_k = 5
42+
expected = 2
43+
actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k)
44+
self.assertEqual(expected, actual)
45+
46+
def test_6(self):
47+
nums = [3,3,3]
48+
min_k = 3
49+
max_k = 3
50+
expected = 6
51+
actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k)
52+
self.assertEqual(expected, actual)
53+
54+
55+
if __name__ == '__main__':
56+
unittest.main()

0 commit comments

Comments
 (0)