Skip to content

Commit 78eb49e

Browse files
committed
feat: add solutions to lc problem: No.2762
1 parent db4a6f3 commit 78eb49e

File tree

4 files changed

+207
-3
lines changed

4 files changed

+207
-3
lines changed

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

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,83 @@ func continuousSubarrays(nums []int) (ans int64) {
180180
}
181181
ans += int64(j - i + 1)
182182
}
183-
return
183+
feat: add solutions to lc problem: No.0929 (#3857)
184+
185+
No.0929.Unique Email Addresses return
186+
}
187+
```
188+
189+
<!-- tabs:end -->
190+
191+
<!-- solution:end -->
192+
193+
<!-- solution:start -->
194+
195+
### Solution 2: Monotonic queue + Two Pointers
196+
197+
<!-- tabs:start -->
198+
199+
#### TypeScript
200+
201+
```ts
202+
function continuousSubarrays(nums: number[]): number {
203+
const [minQ, maxQ]: [number[], number[]] = [[], []];
204+
const n = nums.length;
205+
let res = 0;
206+
207+
for (let r = 0, l = 0; r < n; r++) {
208+
const x = nums[r];
209+
while (minQ.length && nums[minQ.at(-1)!] > x) minQ.pop();
210+
while (maxQ.length && nums[maxQ.at(-1)!] < x) maxQ.pop();
211+
minQ.push(r);
212+
maxQ.push(r);
213+
214+
while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
215+
if (maxQ[0] < minQ[0]) {
216+
l = maxQ[0] + 1;
217+
maxQ.shift();
218+
} else {
219+
l = minQ[0] + 1;
220+
minQ.shift();
221+
}
222+
}
223+
224+
res += r - l + 1;
225+
}
226+
227+
return res;
228+
}
229+
```
230+
231+
#### JavaScript
232+
233+
```js
234+
function continuousSubarrays(nums) {
235+
const [minQ, maxQ] = [[], []];
236+
const n = nums.length;
237+
let res = 0;
238+
239+
for (let r = 0, l = 0; r < n; r++) {
240+
const x = nums[r];
241+
while (minQ.length && nums[minQ.at(-1)] > x) minQ.pop();
242+
while (maxQ.length && nums[maxQ.at(-1)] < x) maxQ.pop();
243+
minQ.push(r);
244+
maxQ.push(r);
245+
246+
while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
247+
if (maxQ[0] < minQ[0]) {
248+
l = maxQ[0] + 1;
249+
maxQ.shift();
250+
} else {
251+
l = minQ[0] + 1;
252+
minQ.shift();
253+
}
254+
}
255+
256+
res += r - l + 1;
257+
}
258+
259+
return res;
184260
}
185261
```
186262

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)