Skip to content

Commit 16b6a31

Browse files
authored
Update README_EN.md
1 parent a785380 commit 16b6a31

File tree

1 file changed

+51
-62
lines changed
  • solution/2400-2499/2444.Count Subarrays With Fixed Bounds

1 file changed

+51
-62
lines changed

solution/2400-2499/2444.Count Subarrays With Fixed Bounds/README_EN.md

+51-62
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1: Enumeration of Right Endpoint
68+
### Solution 1: Enumerate the Right Endpoint
6969

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}$.
7171

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.
7373

7474
The specific implementation logic is as follows:
7575

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.
7979

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)$.
8181

8282
<!-- tabs:start -->
8383

@@ -131,10 +131,16 @@ public:
131131
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
132132
long long ans = 0;
133133
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) {
135+
if (nums[i] < minK || nums[i] > maxK) {
136+
k = i;
137+
}
138+
if (nums[i] == minK) {
139+
j1 = i;
140+
}
141+
if (nums[i] == maxK) {
142+
j2 = i;
143+
}
138144
ans += max(0, min(j1, j2) - k);
139145
}
140146
return ans;
@@ -168,23 +174,15 @@ func countSubarrays(nums []int, minK int, maxK int) int64 {
168174

169175
```ts
170176
function countSubarrays(nums: number[], minK: number, maxK: number): number {
171-
let res = 0;
172-
let minIndex = -1;
173-
let maxIndex = -1;
174-
let k = -1;
175-
nums.forEach((num, i) => {
176-
if (num === minK) {
177-
minIndex = i;
178-
}
179-
if (num === maxK) {
180-
maxIndex = i;
181-
}
182-
if (num < minK || num > maxK) {
183-
k = i;
184-
}
185-
res += Math.max(Math.min(minIndex, maxIndex) - k, 0);
186-
});
187-
return res;
177+
let ans = 0;
178+
let [j1, j2, k] = [-1, -1, -1];
179+
for (let i = 0; i < nums.length; ++i) {
180+
if (nums[i] < minK || nums[i] > maxK) k = i;
181+
if (nums[i] === minK) j1 = i;
182+
if (nums[i] === maxK) j2 = i;
183+
ans += Math.max(0, Math.min(j1, j2) - k);
184+
}
185+
return ans;
188186
}
189187
```
190188

@@ -193,54 +191,45 @@ function countSubarrays(nums: number[], minK: number, maxK: number): number {
193191
```rust
194192
impl Solution {
195193
pub fn count_subarrays(nums: Vec<i32>, min_k: i32, max_k: i32) -> i64 {
196-
let mut res = 0;
197-
let mut min_index = -1;
198-
let mut max_index = -1;
199-
let mut k = -1;
200-
for i in 0..nums.len() {
201-
let num = nums[i];
194+
let mut ans: i64 = 0;
195+
let mut j1: i64 = -1;
196+
let mut j2: i64 = -1;
197+
let mut k: i64 = -1;
198+
for (i, &v) in nums.iter().enumerate() {
202199
let i = i as i64;
203-
if num == min_k {
204-
min_index = i;
200+
if v < min_k || v > max_k {
201+
k = i;
205202
}
206-
if num == max_k {
207-
max_index = i;
203+
if v == min_k {
204+
j1 = i;
208205
}
209-
if num < min_k || num > max_k {
210-
k = i;
206+
if v == max_k {
207+
j2 = i;
208+
}
209+
let m = j1.min(j2);
210+
if m > k {
211+
ans += m - k;
211212
}
212-
res += (0).max(min_index.min(max_index) - k);
213213
}
214-
res
214+
ans
215215
}
216216
}
217217
```
218218

219219
#### C
220220

221221
```c
222-
#define max(a, b) (((a) > (b)) ? (a) : (b))
223-
#define min(a, b) (((a) < (b)) ? (a) : (b))
224-
225222
long long countSubarrays(int* nums, int numsSize, int minK, int maxK) {
226-
long long res = 0;
227-
int minIndex = -1;
228-
int maxIndex = -1;
229-
int k = -1;
230-
for (int i = 0; i < numsSize; i++) {
231-
int num = nums[i];
232-
if (num == minK) {
233-
minIndex = i;
234-
}
235-
if (num == maxK) {
236-
maxIndex = i;
237-
}
238-
if (num < minK || num > maxK) {
239-
k = i;
240-
}
241-
res += max(min(minIndex, maxIndex) - k, 0);
223+
long long ans = 0;
224+
int j1 = -1, j2 = -1, k = -1;
225+
for (int i = 0; i < numsSize; ++i) {
226+
if (nums[i] < minK || nums[i] > maxK) k = i;
227+
if (nums[i] == minK) j1 = i;
228+
if (nums[i] == maxK) j2 = i;
229+
int m = j1 < j2 ? j1 : j2;
230+
if (m > k) ans += (long long) (m - k);
242231
}
243-
return res;
232+
return ans;
244233
}
245234
```
246235

0 commit comments

Comments
 (0)