-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit #274
Comments
The problem asks for the longest subarray where the absolute difference between any two elements is less than or equal to a given Key Points:
Approach:
Plan:
Let's implement this solution in PHP: 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit <?php
/**
* @param Integer[] $nums
* @param Integer $limit
* @return Integer
*/
function longestSubarray($nums, $limit) {
$ans = 1;
$minQ = new SplDoublyLinkedList();
$maxQ = new SplDoublyLinkedList();
for ($l = 0, $r = 0; $r < count($nums); ++$r) {
while (!$minQ->isEmpty() && $minQ->top() > $nums[$r])
$minQ->pop();
$minQ->push($nums[$r]);
while (!$maxQ->isEmpty() && $maxQ->top() < $nums[$r])
$maxQ->pop();
$maxQ->push($nums[$r]);
while ($maxQ->bottom() - $minQ->bottom() > $limit) {
if ($minQ->bottom() == $nums[$l])
$minQ->shift();
if ($maxQ->bottom() == $nums[$l])
$maxQ->shift();
++$l;
}
$ans = max($ans, $r - $l + 1);
}
return $ans;
}
// Example usage:
$nums = [8,2,4,7];
$limit = 4;
echo longestSubarray($nums, $limit) . "\n"; // Output: 2
$nums = [10,1,2,4,7,2];
$limit = 5;
echo longestSubarray($nums, $limit) . "\n"; // Output: 4
$nums = [4,2,2,2,4,4,2,2];
$limit = 0;
echo longestSubarray($nums, $limit) . "\n"; // Output: 3
?> Explanation:
Example Walkthrough:Example 1:Input: nums = [8, 2, 4, 7], limit = 4
Example 2:Input: nums = [10, 1, 2, 4, 7, 2], limit = 5
Time Complexity:
Output for Example:For Example 1:
For Example 2:
This approach efficiently finds the longest subarray that satisfies the condition using a sliding window technique with two deques. The sliding window ensures that we only need to traverse the array once, making the solution scalable for large inputs. The deques allow constant-time access to the maximum and minimum values, making this approach optimal for the problem constraints. |
…absolute-diff-less-than-or-equal-to-limit submissions 1297931948 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
…absolute-diff-less-than-or-equal-to-limit submissions 1297931948 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
Discussed in #273
Originally posted by mah-shamim August 9, 2024
Topics:
Array
,Queue
,Sliding Window
,Heap (Priority Queue)
,Ordered Set
,Monotonic Queue
Given an array of integers
nums
and an integerlimit
, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal tolimit
.Example 1:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.\
Example 2:
Example 3:
Example 4:
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
0 <= limit <= 109
Hint:
The text was updated successfully, but these errors were encountered: