Skip to content

Commit 77a5a49

Browse files
committed
[LeetCode Sync] Runtime - 119 ms (99.05%), Memory - 27.1 MB (40.52%)
1 parent fba71c0 commit 77a5a49

File tree

2 files changed

+78
-0
lines changed
  • Solutions/1549-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit

2 files changed

+78
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input:</strong> nums = [8,2,4,7], limit = 4
8+
<strong>Output:</strong> 2
9+
<strong>Explanation:</strong> All subarrays are:
10+
[8] with maximum absolute diff |8-8| = 0 &lt;= 4.
11+
[8,2] with maximum absolute diff |8-2| = 6 &gt; 4.
12+
[8,2,4] with maximum absolute diff |8-2| = 6 &gt; 4.
13+
[8,2,4,7] with maximum absolute diff |8-2| = 6 &gt; 4.
14+
[2] with maximum absolute diff |2-2| = 0 &lt;= 4.
15+
[2,4] with maximum absolute diff |2-4| = 2 &lt;= 4.
16+
[2,4,7] with maximum absolute diff |2-7| = 5 &gt; 4.
17+
[4] with maximum absolute diff |4-4| = 0 &lt;= 4.
18+
[4,7] with maximum absolute diff |4-7| = 3 &lt;= 4.
19+
[7] with maximum absolute diff |7-7| = 0 &lt;= 4.
20+
Therefore, the size of the longest subarray is 2.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [10,1,2,4,7,2], limit = 5
27+
<strong>Output:</strong> 4
28+
<strong>Explanation:</strong> The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 &lt;= 5.
29+
</pre>
30+
31+
<p><strong class="example">Example 3:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> nums = [4,2,2,2,4,4,2,2], limit = 0
35+
<strong>Output:</strong> 3
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
43+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
44+
<li><code>0 &lt;= limit &lt;= 10<sup>9</sup></code></li>
45+
</ul>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def longestSubarray(self, nums: List[int], limit: int) -> int:
3+
# sorted_list = SortedList()
4+
# result = j = 0
5+
# for i, val in enumerate(nums):
6+
# sorted_list.add(val)
7+
# while sorted_list[-1] - sorted_list[0] > limit:
8+
# sorted_list.remove(nums[j])
9+
# j += 1
10+
# result = max(result, i - j + 1)
11+
# return result
12+
13+
min_queue = Deque()
14+
max_queue = Deque()
15+
left = 0
16+
n = len(nums)
17+
for right, val in enumerate(nums):
18+
while max_queue and nums[max_queue[-1]] < val:
19+
max_queue.pop()
20+
while min_queue and nums[min_queue[-1]] > val:
21+
min_queue.pop()
22+
23+
min_queue.append(right)
24+
max_queue.append(right)
25+
26+
if nums[max_queue[0]] - nums[min_queue[0]] > limit:
27+
left += 1
28+
if max_queue[0] < left:
29+
max_queue.popleft()
30+
if min_queue[0] < left:
31+
min_queue.popleft()
32+
33+
return n - left

0 commit comments

Comments
 (0)