Skip to content

Commit

Permalink
Time: 88 ms (100.00%), Space: 70.5 MB (57.55%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Mar 4, 2023
1 parent 6cf12fb commit 5bd02ef
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
long long ans = 0;
int j = -1;
int prevMinKIndex = -1;
int prevMaxKIndex = -1;

for (int i = 0; i < nums.size(); ++i) {
if (nums[i] < minK || nums[i] > maxK)
j = i;
if (nums[i] == minK)
prevMinKIndex = i;
if (nums[i] == maxK)
prevMaxKIndex = i;
ans += max(0, min(prevMinKIndex, prevMaxKIndex) - j);
}

return ans;
}

};

0 comments on commit 5bd02ef

Please sign in to comment.