You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README_EN.md
+51-62
Original file line number
Diff line number
Diff line change
@@ -65,19 +65,19 @@ tags:
65
65
66
66
<!-- solution:start -->
67
67
68
-
### Solution 1: Enumeration of Right Endpoint
68
+
### Solution 1: Enumerate the Right Endpoint
69
69
70
-
From the problem description, we know that all elements of the bounded subarray are in the interval `[minK, maxK]`, and the minimum value must be `minK`, and the maximum value must be `maxK`.
70
+
According to the problem description, we know that all elements of a bounded subarray are within the range $[\textit{minK}, \textit{maxK}]$, and the minimum value must be $\textit{minK}$, while the maximum value must be $\textit{maxK}$.
71
71
72
-
We traverse the array $nums$, count the number of bounded subarrays with `nums[i]` as the right endpoint, and then add all the counts.
72
+
We iterate through the array $\textit{nums}$ and count the number of bounded subarrays with $\textit{nums}[i]$ as the right endpoint. Then, we sum up all the counts.
73
73
74
74
The specific implementation logic is as follows:
75
75
76
-
1. Maintain the index $k$ of the most recent element not in the interval `[minK, maxK]`, initially set to $-1$. Therefore, the left endpoint of the current element `nums[i]` must be greater than $k$.
77
-
1. Maintain the index $j_1$ of the most recent element with a value of `minK`, and the index $j_2$ of the most recent element with a value of `maxK`, both initially set to $-1$. Therefore, the left endpoint of the current element `nums[i]` must be less than or equal to $\min(j_1, j_2)$.
78
-
1. In summary, the number of bounded subarrays with the current element as the right endpoint is $\max(0, \min(j_1, j_2) - k)$. Add up all the counts to get the result.
76
+
1. Maintain the index $k$ of the most recent element that is not within the range $[\textit{minK}, \textit{maxK}]$, initialized to $-1$. The left endpoint of the current element $\textit{nums}[i]$ must be greater than $k$.
77
+
2. Maintain the most recent index $j_1$ where the value is $\textit{minK}$ and the most recent index $j_2$ where the value is $\textit{maxK}$, both initialized to $-1$. The left endpoint of the current element $\textit{nums}[i]$ must be less than or equal to $\min(j_1, j_2)$.
78
+
3. Based on the above, the number of bounded subarrays with the current element as the right endpoint is $\max\bigl(0,\ \min(j_1, j_2) - k\bigr)$. Accumulate all these counts to get the result.
79
79
80
-
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$.
80
+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
81
81
82
82
<!-- tabs:start -->
83
83
@@ -131,10 +131,16 @@ public:
131
131
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
132
132
long long ans = 0;
133
133
int j1 = -1, j2 = -1, k = -1;
134
-
for (int i = 0; i < nums.size(); ++i) {
135
-
if (nums[i] < minK || nums[i] > maxK) k = i;
136
-
if (nums[i] == minK) j1 = i;
137
-
if (nums[i] == maxK) j2 = i;
134
+
for (int i = 0; i < static_cast<int>(nums.size()); ++i) {
0 commit comments