Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02d60b0

Browse files
authored
feat: add solutions to lc problem: No.2762 (doocs#3859)
1 parent db4a6f3 commit 02d60b0

File tree

4 files changed

+204
-3
lines changed

4 files changed

+204
-3
lines changed

‎solution/2700-2799/2762.Continuous Subarrays/README.md‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,80 @@ func continuousSubarrays(nums []int) (ans int64) {
180180
}
181181
ans += int64(j - i + 1)
182182
}
183-
return
183+
}
184+
```
185+
186+
<!-- tabs:end -->
187+
188+
<!-- solution:end -->
189+
190+
<!-- solution:start -->
191+
192+
### 方法二:单调队列 + 双指针
193+
194+
<!-- tabs:start -->
195+
196+
#### TypeScript
197+
198+
```ts
199+
function continuousSubarrays(nums: number[]): number {
200+
const [minQ, maxQ]: [number[], number[]] = [[], []];
201+
const n = nums.length;
202+
let res = 0;
203+
204+
for (let r = 0, l = 0; r < n; r++) {
205+
const x = nums[r];
206+
while (minQ.length && nums[minQ.at(-1)!] > x) minQ.pop();
207+
while (maxQ.length && nums[maxQ.at(-1)!] < x) maxQ.pop();
208+
minQ.push(r);
209+
maxQ.push(r);
210+
211+
while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
212+
if (maxQ[0] < minQ[0]) {
213+
l = maxQ[0] + 1;
214+
maxQ.shift();
215+
} else {
216+
l = minQ[0] + 1;
217+
minQ.shift();
218+
}
219+
}
220+
221+
res += r - l + 1;
222+
}
223+
224+
return res;
225+
}
226+
```
227+
228+
#### JavaScript
229+
230+
```js
231+
function continuousSubarrays(nums) {
232+
const [minQ, maxQ] = [[], []];
233+
const n = nums.length;
234+
let res = 0;
235+
236+
for (let r = 0, l = 0; r < n; r++) {
237+
const x = nums[r];
238+
while (minQ.length && nums[minQ.at(-1)] > x) minQ.pop();
239+
while (maxQ.length && nums[maxQ.at(-1)] < x) maxQ.pop();
240+
minQ.push(r);
241+
maxQ.push(r);
242+
243+
while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
244+
if (maxQ[0] < minQ[0]) {
245+
l = maxQ[0] + 1;
246+
maxQ.shift();
247+
} else {
248+
l = minQ[0] + 1;
249+
minQ.shift();
250+
}
251+
}
252+
253+
res += r - l + 1;
254+
}
255+
256+
return res;
184257
}
185258
```
186259

‎solution/2700-2799/2762.Continuous Subarrays/README_EN.md‎

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tags:
3939
<pre>
4040
<strong>Input:</strong> nums = [5,4,2,4]
4141
<strong>Output:</strong> 8
42-
<strong>Explanation:</strong>
42+
<strong>Explanation:</strong>
4343
Continuous subarray of size 1: [5], [4], [2], [4].
4444
Continuous subarray of size 2: [5,4], [4,2], [2,4].
4545
Continuous subarray of size 3: [4,2,4].
@@ -55,7 +55,7 @@ It can be shown that there are no more continuous subarrays.
5555
<pre>
5656
<strong>Input:</strong> nums = [1,2,3]
5757
<strong>Output:</strong> 6
58-
<strong>Explanation:</strong>
58+
<strong>Explanation:</strong>
5959
Continuous subarray of size 1: [1], [2], [3].
6060
Continuous subarray of size 2: [1,2], [2,3].
6161
Continuous subarray of size 3: [1,2,3].
@@ -188,4 +188,78 @@ func continuousSubarrays(nums []int) (ans int64) {
188188

189189
<!-- solution:end -->
190190

191+
<!-- solution:start -->
192+
193+
### Solution 2: Monotonic queue + Two Pointers
194+
195+
<!-- tabs:start -->
196+
197+
#### TypeScript
198+
199+
```ts
200+
function continuousSubarrays(nums: number[]): number {
201+
const [minQ, maxQ]: [number[], number[]] = [[], []];
202+
const n = nums.length;
203+
let res = 0;
204+
205+
for (let r = 0, l = 0; r < n; r++) {
206+
const x = nums[r];
207+
while (minQ.length && nums[minQ.at(-1)!] > x) minQ.pop();
208+
while (maxQ.length && nums[maxQ.at(-1)!] < x) maxQ.pop();
209+
minQ.push(r);
210+
maxQ.push(r);
211+
212+
while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
213+
if (maxQ[0] < minQ[0]) {
214+
l = maxQ[0] + 1;
215+
maxQ.shift();
216+
} else {
217+
l = minQ[0] + 1;
218+
minQ.shift();
219+
}
220+
}
221+
222+
res += r - l + 1;
223+
}
224+
225+
return res;
226+
}
227+
```
228+
229+
#### JavaScript
230+
231+
```js
232+
function continuousSubarrays(nums) {
233+
const [minQ, maxQ] = [[], []];
234+
const n = nums.length;
235+
let res = 0;
236+
237+
for (let r = 0, l = 0; r < n; r++) {
238+
const x = nums[r];
239+
while (minQ.length && nums[minQ.at(-1)] > x) minQ.pop();
240+
while (maxQ.length && nums[maxQ.at(-1)] < x) maxQ.pop();
241+
minQ.push(r);
242+
maxQ.push(r);
243+
244+
while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
245+
if (maxQ[0] < minQ[0]) {
246+
l = maxQ[0] + 1;
247+
maxQ.shift();
248+
} else {
249+
l = minQ[0] + 1;
250+
minQ.shift();
251+
}
252+
}
253+
254+
res += r - l + 1;
255+
}
256+
257+
return res;
258+
}
259+
```
260+
261+
<!-- tabs:end -->
262+
263+
<!-- solution:end -->
264+
191265
<!-- problem:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function continuousSubarrays(nums) {
2+
const [minQ, maxQ] = [[], []];
3+
const n = nums.length;
4+
let res = 0;
5+
6+
for (let r = 0, l = 0; r < n; r++) {
7+
const x = nums[r];
8+
while (minQ.length && nums[minQ.at(-1)] > x) minQ.pop();
9+
while (maxQ.length && nums[maxQ.at(-1)] < x) maxQ.pop();
10+
minQ.push(r);
11+
maxQ.push(r);
12+
13+
while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
14+
if (maxQ[0] < minQ[0]) {
15+
l = maxQ[0] + 1;
16+
maxQ.shift();
17+
} else {
18+
l = minQ[0] + 1;
19+
minQ.shift();
20+
}
21+
}
22+
23+
res += r - l + 1;
24+
}
25+
26+
return res;
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function continuousSubarrays(nums: number[]): number {
2+
const [minQ, maxQ]: [number[], number[]] = [[], []];
3+
const n = nums.length;
4+
let res = 0;
5+
6+
for (let r = 0, l = 0; r < n; r++) {
7+
const x = nums[r];
8+
while (minQ.length && nums[minQ.at(-1)!] > x) minQ.pop();
9+
while (maxQ.length && nums[maxQ.at(-1)!] < x) maxQ.pop();
10+
minQ.push(r);
11+
maxQ.push(r);
12+
13+
while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
14+
if (maxQ[0] < minQ[0]) {
15+
l = maxQ[0] + 1;
16+
maxQ.shift();
17+
} else {
18+
l = minQ[0] + 1;
19+
minQ.shift();
20+
}
21+
}
22+
23+
res += r - l + 1;
24+
}
25+
26+
return res;
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /