diff --git a/solution/2700-2799/2762.Continuous Subarrays/README.md b/solution/2700-2799/2762.Continuous Subarrays/README.md index b9b3fcb68402a..3953238032644 100644 --- a/solution/2700-2799/2762.Continuous Subarrays/README.md +++ b/solution/2700-2799/2762.Continuous Subarrays/README.md @@ -180,7 +180,80 @@ func continuousSubarrays(nums []int) (ans int64) { } ans += int64(j - i + 1) } - return +} +``` + + + + + + + +### 方法二:单调队列 + 双指针 + + + +#### 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; } ``` diff --git a/solution/2700-2799/2762.Continuous Subarrays/README_EN.md b/solution/2700-2799/2762.Continuous Subarrays/README_EN.md index 0e75cdd023e96..993537a9aa15b 100644 --- a/solution/2700-2799/2762.Continuous Subarrays/README_EN.md +++ b/solution/2700-2799/2762.Continuous Subarrays/README_EN.md @@ -39,7 +39,7 @@ tags:
 Input: nums = [5,4,2,4]
 Output: 8
-Explanation: 
+Explanation:
 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].
@@ -55,7 +55,7 @@ It can be shown that there are no more continuous subarrays.
 
 Input: nums = [1,2,3]
 Output: 6
-Explanation: 
+Explanation:
 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].
@@ -188,4 +188,78 @@ func continuousSubarrays(nums []int) (ans int64) {
 
 
 
+
+
+### Solution 2: Monotonic queue + Two Pointers
+
+
+
+#### 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;
+}
+```
+
+
+
+
+
 
diff --git a/solution/2700-2799/2762.Continuous Subarrays/Solution2.js b/solution/2700-2799/2762.Continuous Subarrays/Solution2.js
new file mode 100644
index 0000000000000..9d6dd6e44fdf5
--- /dev/null
+++ b/solution/2700-2799/2762.Continuous Subarrays/Solution2.js	
@@ -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;
+}
diff --git a/solution/2700-2799/2762.Continuous Subarrays/Solution2.ts b/solution/2700-2799/2762.Continuous Subarrays/Solution2.ts
new file mode 100644
index 0000000000000..70f48870a6c19
--- /dev/null
+++ b/solution/2700-2799/2762.Continuous Subarrays/Solution2.ts	
@@ -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;
+}