diff --git a/DIRECTORY.md b/DIRECTORY.md index 94efe42a..495f67b4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -196,6 +196,8 @@ * [Min Increment For Unique](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/arrays/minincrementsforunique/min_increment_for_unique.py) * Non Overlapping Intervals * [Test Non Overlapping Intervals](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/arrays/non_overlapping_intervals/test_non_overlapping_intervals.py) + * Subarrays With Fixed Bounds + * [Test Subarrays With Fixed Bounds](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py) * Dicts * [Default Dicts](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/dicts/default_dicts.py) * [Ordered Dict](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/dicts/ordered_dict.py) diff --git a/datastructures/arrays/subarrays_with_fixed_bounds/README.md b/datastructures/arrays/subarrays_with_fixed_bounds/README.md new file mode 100644 index 00000000..090c666d --- /dev/null +++ b/datastructures/arrays/subarrays_with_fixed_bounds/README.md @@ -0,0 +1,22 @@ +# Count Subarrays with Fixed Bounds + +Given an integer array, nums, and two integers minK and maxK, return the number of fixed-bound subarrays. + +A subarray in nums is called a fixed-bound subarray if it satisfies the following conditions: + +1. The smallest value in the subarray equals `minK`. +2. The largest value in the subarray equals `maxK`. + +> Note: A subarray is a contiguous sequence of elements within an array. + +Constraints: + +- 2 ≤ nums.length ≤ 10^3 +- 1 ≤ nums[i], minK, maxK ≤ 10^3 + +## Examples + +![Example One](./images/subarrays_with_fixed_bounds_example_one.png) +![Example Two](./images/subarrays_with_fixed_bounds_example_two.png) +![Example Three](./images/subarrays_with_fixed_bounds_example_three.png) +![Example Four](./images/subarrays_with_fixed_bounds_example_four.png) diff --git a/datastructures/arrays/subarrays_with_fixed_bounds/__init__.py b/datastructures/arrays/subarrays_with_fixed_bounds/__init__.py new file mode 100644 index 00000000..a725d9fd --- /dev/null +++ b/datastructures/arrays/subarrays_with_fixed_bounds/__init__.py @@ -0,0 +1,46 @@ +from typing import List + + +def count_subarrays(nums: List[int], min_k: int, max_k: int) -> int: + """ + This counts the number of subarrays in the provided array which satisfies the following conditions: + + 1. The smallest value in the subarray equals min_k. + 2. The largest value in the subarray equals max_k. + + Args: + nums (List[int]): The array of integers. + min_k (int): The minimum value in the subarray. + max_k (int): The maximum value in the subarray. + + Returns: + int: The number of subarrays that satisfies the conditions. + + Examples: + >>> count_subarrays([2,1,4,3,2],2,3) + 1 + >>> count_subarrays([1,2,3,2,1],1,3) + 5 + >>> count_subarrays([4,4,4],4,4) + 6 + >>> count_subarrays([2,2,2],4,4) + 0 + """ + # edge case to handle empty arrays or invalid bounds + if not nums or min_k > max_k: + return 0 + + last_min, last_max, last_invalid = -1, -1, -1 + count = 0 + + for i, num in enumerate(nums): + if num == min_k: + last_min = i + if num == max_k: + last_max = i + if num < min_k or num > max_k: + last_invalid = i + + count += max(0, min(last_min, last_max) - last_invalid) + + return count diff --git a/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_four.png b/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_four.png new file mode 100644 index 00000000..15659cf7 Binary files /dev/null and b/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_four.png differ diff --git a/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_one.png b/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_one.png new file mode 100644 index 00000000..ab6aa856 Binary files /dev/null and b/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_one.png differ diff --git a/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_three.png b/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_three.png new file mode 100644 index 00000000..d3602d54 Binary files /dev/null and b/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_three.png differ diff --git a/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_two.png b/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_two.png new file mode 100644 index 00000000..7265ea6e Binary files /dev/null and b/datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_two.png differ diff --git a/datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py b/datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py new file mode 100644 index 00000000..73e1f7cd --- /dev/null +++ b/datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py @@ -0,0 +1,56 @@ +import unittest +from . import count_subarrays + + +class SubarraysWithFixedBoundsTestCase(unittest.TestCase): + def test_1(self): + nums = [2,1,4,3,2] + min_k = 2 + max_k = 3 + expected = 1 + actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k) + self.assertEqual(expected, actual) + + def test_2(self): + nums = [1,2,3,2,1] + min_k = 1 + max_k = 3 + expected = 5 + actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k) + self.assertEqual(expected, actual) + + def test_3(self): + nums = [4,4,4] + min_k = 4 + max_k = 4 + expected = 6 + actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k) + self.assertEqual(expected, actual) + + def test_4(self): + nums = [2,2,2] + min_k = 4 + max_k = 4 + expected = 0 + actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k) + self.assertEqual(expected, actual) + + def test_5(self): + nums = [1,3,5,2,7,5] + min_k = 1 + max_k = 5 + expected = 2 + actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k) + self.assertEqual(expected, actual) + + def test_6(self): + nums = [3,3,3] + min_k = 3 + max_k = 3 + expected = 6 + actual = count_subarrays(nums=nums, min_k=min_k, max_k=max_k) + self.assertEqual(expected, actual) + + +if __name__ == '__main__': + unittest.main()