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 c735dbf

Browse files
Merge pull request #51 from codewithhridoy/javascript
medium: 1438. Longest Continuous Subarray With Absolute Diff Less Tha...
2 parents d041197 + fac8e1e commit c735dbf

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} limit
4+
* @return {number}
5+
*/
6+
function longestSubarray(nums, limit) {
7+
let maxDeque = [];
8+
let minDeque = [];
9+
let left = 0;
10+
let result = 0;
11+
12+
for (let right = 0; right < nums.length; right++) {
13+
// Maintain the maxDeque for the maximum value in the current window
14+
while (maxDeque.length && nums[maxDeque[maxDeque.length - 1]] <= nums[right]) {
15+
maxDeque.pop();
16+
}
17+
maxDeque.push(right);
18+
19+
// Maintain the minDeque for the minimum value in the current window
20+
while (minDeque.length && nums[minDeque[minDeque.length - 1]] >= nums[right]) {
21+
minDeque.pop();
22+
}
23+
minDeque.push(right);
24+
25+
// Check if the current window is valid
26+
while (nums[maxDeque[0]] - nums[minDeque[0]] > limit) {
27+
left++;
28+
if (left > maxDeque[0]) {
29+
maxDeque.shift();
30+
}
31+
if (left > minDeque[0]) {
32+
minDeque.shift();
33+
}
34+
}
35+
36+
// Update the result with the size of the current valid window
37+
result = Math.max(result, right - left + 1);
38+
}
39+
40+
return result;
41+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit)
2+
3+
---
4+
5+
title: "Finding the Longest Subarray with Absolute Diff Less than Limit"
6+
summary: "An efficient approach to finding the longest subarray with an absolute difference less than a specified limit using deques."
7+
date: "2023年06月23日"
8+
modified_date: "2023年06月23日"
9+
tags: ["algorithm", "sliding window", "deques", "javascript"]
10+
slug: "longest-subarray-absolute-diff"
11+
12+
---
13+
14+
![image.png](https://assets.leetcode.com/users/images/53c7dbfe-f60e-4ce7-b722-e0c89b1d8f95_1719150350.5995736.png)
15+
16+
# Intuition
17+
18+
To solve this problem, the first step is to identify that we need to find a subarray where the difference between the maximum and minimum elements is less than or equal to a given limit. The problem can be effectively approached using a sliding window technique combined with deques to keep track of the maximum and minimum elements within the window.
19+
20+
# Approach
21+
22+
We use two deques: one to keep track of the maximum values and the other for the minimum values within the current window. The deques help us efficiently maintain the maximum and minimum values as we expand and contract the sliding window.
23+
24+
1. Initialize two deques, `maxDeque` and `minDeque`, to store indices of array elements in decreasing and increasing order respectively.
25+
2. Iterate through the array with a sliding window defined by two pointers, `left` and `right`.
26+
3. For each element at `right`, update the deques to include this element while maintaining their order properties.
27+
4. Check if the current window satisfies the condition where the absolute difference between the maximum and minimum elements is less than or equal to the limit. If not, increment the `left` pointer to reduce the window size and maintain the condition.
28+
5. Keep track of the maximum window size that satisfies the condition.
29+
30+
# Complexity
31+
32+
- Time complexity: $$O(n)$$
33+
The algorithm processes each element of the array at most twice, resulting in a linear time complexity.
34+
35+
- Space complexity: $$O(n)$$
36+
In the worst case, the deques can store all elements of the array, leading to a linear space complexity.
37+
38+
# Code
39+
40+
```javascript
41+
/**
42+
* @param {number[]} nums
43+
* @param {number} limit
44+
* @return {number}
45+
*/
46+
function longestSubarray(nums, limit) {
47+
let maxDeque = [];
48+
let minDeque = [];
49+
let left = 0;
50+
let result = 0;
51+
52+
for (let right = 0; right < nums.length; right++) {
53+
// Maintain the maxDeque for the maximum value in the current window
54+
while (
55+
maxDeque.length &&
56+
nums[maxDeque[maxDeque.length - 1]] <= nums[right]
57+
) {
58+
maxDeque.pop();
59+
}
60+
maxDeque.push(right);
61+
62+
// Maintain the minDeque for the minimum value in the current window
63+
while (
64+
minDeque.length &&
65+
nums[minDeque[minDeque.length - 1]] >= nums[right]
66+
) {
67+
minDeque.pop();
68+
}
69+
minDeque.push(right);
70+
71+
// Check if the current window is valid
72+
while (nums[maxDeque[0]] - nums[minDeque[0]] > limit) {
73+
left++;
74+
if (left > maxDeque[0]) {
75+
maxDeque.shift();
76+
}
77+
if (left > minDeque[0]) {
78+
minDeque.shift();
79+
}
80+
}
81+
82+
// Update the result with the size of the current valid window
83+
result = Math.max(result, right - left + 1);
84+
}
85+
86+
return result;
87+
}
88+
```

0 commit comments

Comments
(0)

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