From 26c0b009bd2bf8167155957d0e21d5b468fd3d54 Mon Sep 17 00:00:00 2001 From: chayan das Date: Sat, 26 Apr 2025 16:19:35 +0530 Subject: [PATCH] Create 2444. Count Subarrays With Fixed Bounds1 --- 2444. Count Subarrays With Fixed Bounds1 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2444. Count Subarrays With Fixed Bounds1 diff --git a/2444. Count Subarrays With Fixed Bounds1 b/2444. Count Subarrays With Fixed Bounds1 new file mode 100644 index 0000000..c79a271 --- /dev/null +++ b/2444. Count Subarrays With Fixed Bounds1 @@ -0,0 +1,24 @@ +class Solution { +public: + long long countSubarrays(vector& nums, int minK, int maxK) { + long long count = 0; + int lastMinK = -1, lastMaxK = -1, lastInvalid = -1; + + for (int i = 0; i < nums.size(); i++) { + if (nums[i] < minK || nums[i] > maxK) { + lastInvalid = i; + } + if (nums[i] == minK) { + lastMinK = i; + } + if (nums[i] == maxK) { + lastMaxK = i; + } + if (lastMinK != -1 && lastMaxK != -1) { + count += max(0, min(lastMinK, lastMaxK) - lastInvalid); + } + } + + return count; + } +};