From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/subarr_fixed_bound.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 other/subarr_fixed_bound.cpp (limited to 'other/subarr_fixed_bound.cpp') diff --git a/other/subarr_fixed_bound.cpp b/other/subarr_fixed_bound.cpp new file mode 100644 index 0000000..658c766 --- /dev/null +++ b/other/subarr_fixed_bound.cpp @@ -0,0 +1,33 @@ +// 2444. Count Subarrays With Fixed Bounds +#include "leetcode.h" + +/* + * given integer array 'nums' and two integers 'min_k' and 'max_k'. a fixed + * bound subarr of 'nums' is a subarray that satisfies the following conditions: + * minimum value in subarray is equal to 'min_k'. the max value in subarray is + * equal to 'max_k'. return number of fixed-bound subarrays. + */ + +class Solution { +public: + long long countSubarrays(vector &nums, int min_k, int max_k) { + long long ans = 0, n = nums.size(); + for (int i = 0, j = 0; i <= n; ++i) + if (i == n || nums[i] < min_k || nums[i] > max_k) + for (int p1 = j, p2 = j; j <= i; ++j) { + while (p1 < i && (p1 < j || nums[p1] != min_k)) + ++p1; + while (p2 < i && (p2 < j || nums[p2] != max_k)) + ++p2; + ans += i - max(p1, p2); + } + return ans; + } +}; + +int main() { + Solution obj; + vector n1 = {1, 3, 5, 2, 7, 5}, n2 = {1, 1, 1, 1}; + cout << obj.countSubarrays(n1, 1, 5) << endl; // expect: 2 + cout << obj.countSubarrays(n2, 1, 1) << endl; // expect: 10 +} -- cgit v1.2.3