aboutsummaryrefslogtreecommitdiff
path: root/other/subarr_fixed_bound.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/subarr_fixed_bound.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/subarr_fixed_bound.cpp')
-rw-r--r--other/subarr_fixed_bound.cpp33
1 files changed, 33 insertions, 0 deletions
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<int> &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<int> 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
+}