Skip to content

Commit 1005257

Browse files
authored
Merge pull request #100 from BrianLusina/feat/subarrays-fixed-bounds
feat(arrays) subarrays with fixed bounds
2 parents eb9734c + b497e84 commit 1005257

File tree

8 files changed

+126
-0
lines changed

8 files changed

+126
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@
196196
* [Min Increment For Unique](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/arrays/minincrementsforunique/min_increment_for_unique.py)
197197
* Non Overlapping Intervals
198198
* [Test Non Overlapping Intervals](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/arrays/non_overlapping_intervals/test_non_overlapping_intervals.py)
199+
* Subarrays With Fixed Bounds
200+
* [Test Subarrays With Fixed Bounds](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py)
199201
* Dicts
200202
* [Default Dicts](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/dicts/default_dicts.py)
201203
* [Ordered Dict](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/dicts/ordered_dict.py)
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import List
2+
3+
4+
def count_subarrays(nums: List[int], min_k: int, max_k: int) -> int:
5+
"""
6+
This counts the number of subarrays in the provided array which satisfies the following conditions:
7+
8+
1. The smallest value in the subarray equals min_k.
9+
2. The largest value in the subarray equals max_k.
10+
11+
Args:
12+
nums (List[int]): The array of integers.
13+
min_k (int): The minimum value in the subarray.
14+
max_k (int): The maximum value in the subarray.
15+
16+
Returns:
17+
int: The number of subarrays that satisfies the conditions.
18+
19+
Examples:
20+
>>> count_subarrays([2,1,4,3,2],2,3)
21+
1
22+
>>> count_subarrays([1,2,3,2,1],1,3)
23+
5
24+
>>> count_subarrays([4,4,4],4,4)
25+
6
26+
>>> count_subarrays([2,2,2],4,4)
27+
0
28+
"""
29+
# edge case to handle empty arrays or invalid bounds
30+
if not nums or min_k > max_k:
31+
return 0
32+
33+
last_min, last_max, last_invalid = -1, -1, -1
34+
count = 0
35+
36+
for i, num in enumerate(nums):
37+
if num == min_k:
38+
last_min = i
39+
if num == max_k:
40+
last_max = i
41+
if num < min_k or num > max_k:
42+
last_invalid = i
43+
44+
count += max(0, min(last_min, last_max) - last_invalid)
45+
46+
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)