Skip to content

Commit 2cb0bbf

Browse files
committed
Runtime: 46 ms (Top 85.16%) | Memory: 52.70 MB (Top 42.49%)
1 parent a6c68bf commit 2cb0bbf

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
// Runtime: 46 ms (Top 85.16%) | Memory: 52.70 MB (Top 42.49%)
2+
13
class Solution {
24
public:
35
int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {
4-
int ans = 0; // store ans
5-
int j=-1; // starting window
6-
int sub = 0; // if current element is less than left bound then count how may element before current element which is less than left and must be continues(it means any element which is greater than left bound reset the count to 0 )
7-
for(int i=0;i<nums.size();i++){
8-
if(nums[i]>right){
9-
j = i;
10-
sub = 0;
6+
int n = nums.size();
7+
int si =0;//starting index
8+
int ei = 0; //ending index
9+
int result =0, prev_count =0;
10+
while(ei < n){
11+
if(left <= nums[ei] && nums[ei]<= right){
12+
prev_count = ei- si +1;
13+
result += prev_count;
14+
}else if ( nums[ei] < left){
15+
result += prev_count;
16+
}else{
17+
//right < nums[ei]
18+
si = ei +1;
19+
prev_count =0;
1120
}
12-
else if(nums[i]<left){
13-
sub++;
14-
}
15-
else sub = 0;
16-
ans = ans + i - j - sub;
17-
}
18-
return ans;
21+
ei++;
22+
}
23+
return result;
1924
}
2025
};

0 commit comments

Comments
 (0)