Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion solution/2700-2799/2762.Continuous Subarrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,80 @@ func continuousSubarrays(nums []int) (ans int64) {
}
ans += int64(j - i + 1)
}
return
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:单调队列 + 双指针

<!-- tabs:start -->

#### TypeScript

```ts
function continuousSubarrays(nums: number[]): number {
const [minQ, maxQ]: [number[], number[]] = [[], []];
const n = nums.length;
let res = 0;

for (let r = 0, l = 0; r < n; r++) {
const x = nums[r];
while (minQ.length && nums[minQ.at(-1)!] > x) minQ.pop();
while (maxQ.length && nums[maxQ.at(-1)!] < x) maxQ.pop();
minQ.push(r);
maxQ.push(r);

while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
if (maxQ[0] < minQ[0]) {
l = maxQ[0] + 1;
maxQ.shift();
} else {
l = minQ[0] + 1;
minQ.shift();
}
}

res += r - l + 1;
}

return res;
}
```

#### JavaScript

```js
function continuousSubarrays(nums) {
const [minQ, maxQ] = [[], []];
const n = nums.length;
let res = 0;

for (let r = 0, l = 0; r < n; r++) {
const x = nums[r];
while (minQ.length && nums[minQ.at(-1)] > x) minQ.pop();
while (maxQ.length && nums[maxQ.at(-1)] < x) maxQ.pop();
minQ.push(r);
maxQ.push(r);

while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
if (maxQ[0] < minQ[0]) {
l = maxQ[0] + 1;
maxQ.shift();
} else {
l = minQ[0] + 1;
minQ.shift();
}
}

res += r - l + 1;
}

return res;
}
```

Expand Down
78 changes: 76 additions & 2 deletions solution/2700-2799/2762.Continuous Subarrays/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [5,4,2,4]
<strong>Output:</strong> 8
<strong>Explanation:</strong>
<strong>Explanation:</strong>
Continuous subarray of size 1: [5], [4], [2], [4].
Continuous subarray of size 2: [5,4], [4,2], [2,4].
Continuous subarray of size 3: [4,2,4].
Expand All @@ -55,7 +55,7 @@ It can be shown that there are no more continuous subarrays.
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
<strong>Explanation:</strong>
Continuous subarray of size 1: [1], [2], [3].
Continuous subarray of size 2: [1,2], [2,3].
Continuous subarray of size 3: [1,2,3].
Expand Down Expand Up @@ -188,4 +188,78 @@ func continuousSubarrays(nums []int) (ans int64) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Monotonic queue + Two Pointers

<!-- tabs:start -->

#### TypeScript

```ts
function continuousSubarrays(nums: number[]): number {
const [minQ, maxQ]: [number[], number[]] = [[], []];
const n = nums.length;
let res = 0;

for (let r = 0, l = 0; r < n; r++) {
const x = nums[r];
while (minQ.length && nums[minQ.at(-1)!] > x) minQ.pop();
while (maxQ.length && nums[maxQ.at(-1)!] < x) maxQ.pop();
minQ.push(r);
maxQ.push(r);

while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
if (maxQ[0] < minQ[0]) {
l = maxQ[0] + 1;
maxQ.shift();
} else {
l = minQ[0] + 1;
minQ.shift();
}
}

res += r - l + 1;
}

return res;
}
```

#### JavaScript

```js
function continuousSubarrays(nums) {
const [minQ, maxQ] = [[], []];
const n = nums.length;
let res = 0;

for (let r = 0, l = 0; r < n; r++) {
const x = nums[r];
while (minQ.length && nums[minQ.at(-1)] > x) minQ.pop();
while (maxQ.length && nums[maxQ.at(-1)] < x) maxQ.pop();
minQ.push(r);
maxQ.push(r);

while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
if (maxQ[0] < minQ[0]) {
l = maxQ[0] + 1;
maxQ.shift();
} else {
l = minQ[0] + 1;
minQ.shift();
}
}

res += r - l + 1;
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
27 changes: 27 additions & 0 deletions solution/2700-2799/2762.Continuous Subarrays/Solution2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function continuousSubarrays(nums) {
const [minQ, maxQ] = [[], []];
const n = nums.length;
let res = 0;

for (let r = 0, l = 0; r < n; r++) {
const x = nums[r];
while (minQ.length && nums[minQ.at(-1)] > x) minQ.pop();
while (maxQ.length && nums[maxQ.at(-1)] < x) maxQ.pop();
minQ.push(r);
maxQ.push(r);

while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
if (maxQ[0] < minQ[0]) {
l = maxQ[0] + 1;
maxQ.shift();
} else {
l = minQ[0] + 1;
minQ.shift();
}
}

res += r - l + 1;
}

return res;
}
27 changes: 27 additions & 0 deletions solution/2700-2799/2762.Continuous Subarrays/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function continuousSubarrays(nums: number[]): number {
const [minQ, maxQ]: [number[], number[]] = [[], []];
const n = nums.length;
let res = 0;

for (let r = 0, l = 0; r < n; r++) {
const x = nums[r];
while (minQ.length && nums[minQ.at(-1)!] > x) minQ.pop();
while (maxQ.length && nums[maxQ.at(-1)!] < x) maxQ.pop();
minQ.push(r);
maxQ.push(r);

while (minQ.length && maxQ.length && nums[maxQ[0]] - nums[minQ[0]] > 2) {
if (maxQ[0] < minQ[0]) {
l = maxQ[0] + 1;
maxQ.shift();
} else {
l = minQ[0] + 1;
minQ.shift();
}
}

res += r - l + 1;
}

return res;
}
Loading