Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions datastructures/arrays/subarrays_with_fixed_bounds/README.md
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 46 additions & 0 deletions datastructures/arrays/subarrays_with_fixed_bounds/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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()
Loading