File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Leetcode Daily Challenge Solutions
2+
3+
## Today's 31-03-24 [Problem Link](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/description/?envType=daily-question&envId=2024-03-31)
4+
## 2444. Count Subarrays With Fixed Bounds
5+
6+
# Intuition
7+
<!-- Describe your first thoughts on how to solve this problem. -->
8+
The problem requires counting the number of subarrays within a given range [minK, maxK] where the subarray's elements are also within this range. To efficiently count these subarrays, I can iterate through the array once and keep track of the last invalid index, the last occurrence of minK, and the last occurrence of maxK. By doing so, I can determine the valid subarrays that end at each index.
9+
10+
# Approach
11+
<!-- Describe your approach to solving the problem. -->
12+
13+
- Initialized variables :
14+
- `count`: to store the count of valid subarrays.
15+
- `lastInvalidIndex`: to keep track of the index of the last element that violates the range condition.
16+
- `lastMinIndex`: to store the index of the last occurrence of minK.
17+
- `lastMaxIndex`: to store the index of the last occurrence of maxK.
18+
19+
- Iterated through the array `nums` :
20+
- Updated `lastInvalidIndex` if the current element is out of the range [minK, maxK].
21+
- Updated `lastMinIndex` if the current element is equal to minK.
22+
- Updated `lastMaxIndex` if the current element is equal to maxK.
23+
- Calculated the count of valid subarrays up to the current index by taking the maximum of 0 and the difference between the minimum of `lastMinIndex` and `lastMaxIndex` and `lastInvalidIndex`.
24+
- Added this count to the `count`.
25+
26+
- Returned the total count of valid subarrays.
27+
28+
By following this approach, I can efficiently count the number of valid subarrays within the given range.
29+
30+
---
31+
Have a look at the code , still have any confusion then please let me know in the comments
32+
Keep Solving.:)
33+
# Complexity
34+
- Time complexity : $O(n)$
35+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
36+
$n$ : length of the input array `nums`
37+
- Space complexity : $O(1)$
38+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
39+
40+
# Code
41+
```
42+
class Solution {
43+
44+
// Function to count subarrays within the given range [minK, maxK]
45+
public long countSubarrays(int[] nums, int minK, int maxK) {
46+
47+
// Variable to store the count of valid subarrays
48+
long count = 0;
49+
// Index of the last element that violates the range condition
50+
int lastInvalidIndex = -1;
51+
// Index of the last occurrence of minK
52+
int lastMinIndex = -1;
53+
// Index of the last occurrence of maxK
54+
int lastMaxIndex = -1;
55+
56+
// Iterating through the array
57+
for (int i = 0; i < nums.length; ++i) {
58+
// Updating lastInvalidIndex if current element is out of range
59+
if (nums[i] < minK || nums[i] > maxK) {
60+
lastInvalidIndex = i;
61+
}
62+
// Updating lastMinIndex if current element is equal to minK
63+
if (nums[i] == minK) {
64+
lastMinIndex = i;
65+
}
66+
// Updating lastMaxIndex if current element is equal to maxK
67+
if (nums[i] == maxK) {
68+
lastMaxIndex = i;
69+
}
70+
// Calculating the count of valid subarrays up to the current index
71+
count += Math.max(0, Math.min(lastMinIndex, lastMaxIndex) - lastInvalidIndex);
72+
}
73+
// Returning the total count of valid subarrays
74+
return count;
75+
}
76+
}
77+
```

0 commit comments

Comments
 (0)