description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
sub = dict()
for i in range(len(s) - minSize + 1):
d = dict()
for k in range(i, i + minSize):
if s[k] in d:
d[s[k]] += 1
else:
d[s[k]] = 1
if len(d) <= maxLetters:
phrase = s[i : i + minSize]
if phrase in sub:
sub[phrase] += 1
else:
sub[phrase] = 1
else:
continue
for j in range(i + minSize, i + maxSize):
if j < len(s):
if len(d) <= maxLetters:
phrase = s[i : j + 1]
if phrase in sub:
sub[phrase] += 1
else:
sub[phrase] = 1
else:
break
if s[j] in d:
d[s[j]] += 1
else:
d[s[j]] = 1
if not sub:
return 0
return max(sub.values())
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
toSearch = {}
for size in range(minSize, maxSize + 1):
for i in range(len(s) - size + 1):
S = s[i : i + size]
letters = len(set(S))
if letters <= maxLetters:
if S in toSearch:
toSearch[S] += 1
else:
toSearch[S] = 1
ans = 0
for e in toSearch:
ans = max(ans, toSearch[e])
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
for i in range(minSize, maxSize + 1):
max_freq = self.maxFreqSetSize(s, maxLetters, i)
if max_freq > 0:
return max_freq
return 0
def maxFreqSetSize(self, s, maxLetters, windowSize):
valid_substrings = set()
maxFreq = 0
for i in range(len(s) - windowSize):
substring = s[i : i + windowSize]
if substring in valid_substrings:
continue
unique_letters = set()
for k in range(windowSize):
letter = s[i + k]
unique_letters.add(letter)
if len(unique_letters) > maxLetters:
continue
frequency = 1
pos = i + 1
while pos > -1:
new_pos = s[pos:].find(substring)
if new_pos == -1:
break
frequency += 1
pos += new_pos + 1
if frequency > maxFreq:
maxFreq = frequency
valid_substrings.add(substring)
return maxFreq
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
feq = collections.Counter()
N = len(s)
for i in range(N):
letters = set([c for c in s[i : i + minSize - 1]])
for j in range(minSize, maxSize + 1):
k = i + j
if k > N:
break
letters.add(s[k - 1])
if len(letters) > maxLetters:
break
feq[s[i:k]] += 1
return max(feq.values()) if list(feq.values()) else 0
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
maxi = 0
for index in range(minSize, maxSize + 1):
result = self.getAllSubstringsWithRules(s, maxLetters, index)
if result > maxi:
maxi = result
return maxi
def getAllSubstringsWithRules(self, s, maxLetters, size):
length = len(s)
letters = {}
subStrings = {}
for index in range(size):
letter = s[index]
if letter not in letters:
letters[letter] = 0
letters[letter] += 1
if len(letters) <= maxLetters:
subStrings[s[:size]] = 1
for index in range(size, length):
letterToRemove = s[index - size]
letters[letterToRemove] -= 1
if letters[letterToRemove] == 0:
del letters[letterToRemove]
letterToAdd = s[index]
if letterToAdd not in letters:
letters[letterToAdd] = 0
letters[letterToAdd] += 1
if len(letters) <= maxLetters:
string = s[index - size + 1 : index + 1]
if string not in subStrings:
subStrings[string] = 0
subStrings[string] += 1
if not subStrings:
return 0
return max(subStrings.values())
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxy: int, m: int, mm: int) -> int:
i, j = 0, 0
count = collections.Counter()
count[s[0]] += 1
ans = collections.Counter()
u, n = 1, len(s)
key = s[0]
while True:
if u <= maxy and m <= i - j + 1 <= mm:
ans[key] += 1
if j < i and i - j + 1 >= m:
count[s[j]] -= 1
if count[s[j]] == 0:
u -= 1
j += 1
key = key[1:]
else:
i += 1
if i == n:
break
if count[s[i]] == 0:
u += 1
count[s[i]] += 1
key += s[i]
return max(ans.values()) if ans else 0
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
cache = collections.defaultdict(int)
for i in range(len(s) + 1 - minSize):
if len(set(s[i : i + minSize])) <= maxLetters:
cache[s[i : i + minSize]] += 1
res = 0
for k, v in list(cache.items()):
res = max(res, v)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
n = len(s)
results = 0
for l in range(minSize, maxSize + 1):
if l <= n:
maps = {}
for i in range(n - l + 1):
subs = s[i : i + l]
if len(set(subs)) <= maxLetters:
try:
maps[subs] += 1
if maps[subs] > results:
results = maps[subs]
except KeyError:
maps[subs] = 1
if maps[subs] > results:
results = maps[subs]
return results
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
candidates = collections.Counter()
for i in range(len(s) - minSize + 1):
if len(set(list(s[i : i + minSize]))) <= maxLetters:
candidates[s[i : i + minSize]] += 1
return max(list(candidates.values()) + [0])
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER VAR
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
mymap = {}
for i in range(len(s) - minSize + 1):
if len(set(list(s[i : i + minSize]))) <= maxLetters:
print(s[i : i + minSize])
if s[i : i + minSize] in mymap:
mymap[s[i : i + minSize]] += 1
else:
mymap[s[i : i + minSize]] = 1
if not mymap:
return 0
return max(mymap.values())
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_q, min_q = [], []
ans = 1
l = 0
for i, num in enumerate(nums):
while len(max_q) and nums[max_q[-1]] < num:
max_q.pop(-1)
while len(min_q) and nums[min_q[-1]] > num:
min_q.pop(-1)
min_q.append(i)
max_q.append(i)
while nums[max_q[0]] - nums[min_q[0]] > limit:
if max_q[0] > min_q[0]:
l = min_q.pop(0) + 1
else:
l = max_q.pop(0) + 1
ans = max(i - l + 1, ans)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
asc, desc = deque(), deque()
left = 0
longest = 0
for i, n in enumerate(nums):
if not asc:
asc.append([n, i])
else:
while asc and asc[-1][0] > n:
asc.pop()
asc.append([n, i])
if not desc:
desc.append([n, i])
else:
while desc and desc[-1][0] < n:
desc.pop()
desc.append([n, i])
while desc[0][0] - asc[0][0] > limit:
if desc[0][1] < asc[0][1]:
left = desc.popleft()[1] + 1
else:
left = asc.popleft()[1] + 1
longest = max(longest, i - left + 1)
return longest
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) == 1:
return 1
maxL = 0
beg = 0
minQueue, maxQueue = collections.deque([]), collections.deque([])
end = beg
while end < len(nums):
while len(minQueue) > 0 and nums[end] < minQueue[-1]:
minQueue.pop()
minQueue.append(nums[end])
while len(maxQueue) > 0 and nums[end] > maxQueue[-1]:
maxQueue.pop()
maxQueue.append(nums[end])
if maxQueue[0] - minQueue[0] <= limit:
maxL = max(maxL, end - beg + 1)
else:
if maxQueue[0] == nums[beg]:
maxQueue.popleft()
if minQueue[0] == nums[beg]:
minQueue.popleft()
beg += 1
end += 1
return maxL
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST FUNC_CALL VAR LIST ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
if len(nums) == 1:
return 1
l = 0
r = 1
curmax = nums[0]
curmin = nums[0]
maxlen = 1
while l <= r and r < len(nums):
curmax = max(curmax, nums[r])
curmin = min(curmin, nums[r])
if curmax - curmin <= limit:
maxlen = max(maxlen, r - l + 1)
else:
if nums[l] == curmax:
curmax = max(nums[l + 1 : r + 1])
if nums[l] == curmin:
curmin = min(nums[l + 1 : r + 1])
l += 1
r += 1
return maxlen
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
decStack = collections.deque()
incStack = collections.deque()
ans = 0
l = 0
for r, n in enumerate(nums):
while decStack and decStack[-1][0] <= n:
decStack.pop()
decStack.append((n, r))
while incStack and incStack[-1][0] >= n:
incStack.pop()
incStack.append((n, r))
while decStack[0][0] - incStack[0][0] > limit:
l = min(incStack[0][1], decStack[0][1]) + 1
while decStack and decStack[0][1] < l:
decStack.popleft()
while incStack and incStack[0][1] < l:
incStack.popleft()
ans = max(ans, r - l + 1)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maximums = [(-nums[0], 0)]
minimums = [(nums[0], 0)]
left = 0 if len(nums) > 1 else 1
right = 1
while right < len(nums):
heapq.heappush(maximums, (-nums[right], right))
heapq.heappush(minimums, (nums[right], right))
right += 1
while maximums[0][1] < left:
heapq.heappop(maximums)
while minimums[0][1] < left:
heapq.heappop(minimums)
while -maximums[0][0] - minimums[0][0] <= limit and right < len(nums):
heapq.heappush(maximums, (-nums[right], right))
heapq.heappush(minimums, (nums[right], right))
right += 1
left += 1
if -maximums[0][0] - minimums[0][0] <= limit:
return right - left + 1
else:
return right - left
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class minMonotonicQueue(object):
def __init__(self):
self.queue = collections.deque([])
def push(self, num, idx):
while len(self.queue) != 0 and self.queue[-1][0] > num:
self.queue.pop()
self.queue.append((num, idx))
def pop(self, idx):
if self.queue[0][1] == idx:
self.queue.popleft()
class maxMonotonicQueue(object):
def __init__(self):
self.queue = collections.deque([])
def push(self, num, idx):
while len(self.queue) != 0 and self.queue[-1][0] < num:
self.queue.pop()
self.queue.append((num, idx))
def pop(self, idx):
if self.queue[0][1] == idx:
self.queue.popleft()
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
ptr1, ptr2 = 0, 0
res = 0
minQ, maxQ = minMonotonicQueue(), maxMonotonicQueue()
while ptr1 < len(nums) and ptr2 < len(nums):
minQ.push(nums[ptr2], ptr2)
maxQ.push(nums[ptr2], ptr2)
while maxQ.queue[0][0] - minQ.queue[0][0] > limit:
ptr1 += 1
if ptr1 > maxQ.queue[0][1]:
maxQ.pop(maxQ.queue[0][1])
if ptr1 > minQ.queue[0][1]:
minQ.pop(minQ.queue[0][1])
res = max(res, ptr2 - ptr1 + 1)
ptr2 += 1
return res
|
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST FUNC_DEF WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST FUNC_DEF WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
low = None
curr_max = None
curr_low = -1
max_len = 0
min_queue = []
max_queue = []
for i in range(len(nums)):
curr_num = nums[i]
self.queue_add_max(max_queue, curr_num, i)
self.queue_add_min(min_queue, curr_num, i)
curr_max = max_queue[0][0]
curr_min = min_queue[0][0]
if curr_max - curr_min > limit:
while curr_max - curr_min > limit and curr_low < i:
curr_low += 1
if curr_low == min_queue[0][1]:
min_queue.pop(0)
if curr_low == max_queue[0][1]:
max_queue.pop(0)
curr_max = max_queue[0][0]
curr_min = min_queue[0][0]
curr_dist = i - curr_low
if curr_dist > max_len:
max_len = curr_dist
return max_len
def queue_add_max(self, queue, element, index):
while queue != []:
if element > queue[len(queue) - 1][0]:
queue.pop()
else:
break
queue.append((element, index))
def queue_add_min(self, queue, element, index):
while queue != []:
if element < queue[len(queue) - 1][0]:
queue.pop()
else:
break
queue.append((element, index))
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF WHILE VAR LIST IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF WHILE VAR LIST IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
l = 0
thing = 0
curMin = 9999999
curMax = -9999999
numMap = {}
i = 0
while i < len(nums):
if nums[i] not in numMap:
numMap[nums[i]] = 0
numMap[nums[i]] += 1
curMax = max(curMax, nums[i])
curMin = min(curMin, nums[i])
i += 1
if curMax - curMin <= limit:
thing = max(thing, i - l)
else:
p = nums[l]
numMap[nums[l]] -= 1
if numMap[nums[l]] == 0:
del numMap[nums[l]]
l = l + 1
if p == curMin or p == curMax:
curMin = min(numMap)
curMax = max(numMap)
return thing
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class MinMaxList(object):
def __init__(self):
self._container = {}
self._max = -6665677
self._min = 6665677
def min_val(self) -> int:
return self._min
def max_val(self) -> int:
return self._max
def append_val(self, val: int):
if val in self._container:
self._container[val] = self._container[val] + 1
else:
self._container[val] = 1
self._max = max(val, self._max)
self._min = min(val, self._min)
def remove_val(self, val: int):
if val in self._container:
if self._container[val] > 1:
self._container[val] = self._container[val] - 1
return
else:
del self._container[val]
if self._max == val:
self._max = max(self._container.keys())
if self._min == val:
self._min = min(self._container.keys())
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
diff = 0
max_result = 0
max_subarray = 0
low = 0
high = 0
window = MinMaxList()
while high < len(nums):
window.append_val(nums[high])
high = high + 1
max_subarray = max_subarray + 1
diff = abs(window.max_val() - window.min_val())
while diff > limit:
window.remove_val(nums[low])
low = low + 1
max_subarray = max_subarray - 1
diff = abs(window.max_val() - window.min_val())
max_result = max(max_subarray, max_result)
return max_result
|
CLASS_DEF VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) == 0:
return 0
max_q = MonoQueue(False)
min_q = MonoQueue(True)
l, r = 0, 0
max_q.push(nums[0])
min_q.push(nums[0])
def in_limit():
if max_q.is_empty() is True:
return True
return abs(max_q.top() - min_q.top()) <= limit
res = 0
while r < len(nums):
if in_limit() is True:
res = max(res, r - l + 1)
r += 1
if r < len(nums):
min_q.push(nums[r])
max_q.push(nums[r])
else:
min_q.popfront(nums[l])
max_q.popfront(nums[l])
l += 1
return res
class MonoQueue:
def __init__(self, inc=True):
self.queue = []
self.inc = inc
def popfront(self, n):
if self.queue[0] == n:
self.queue.pop(0)
return
def push(self, n):
while self.queue:
if self.inc is True and self.queue[-1] > n:
self.queue.pop(-1)
elif self.inc is False and self.queue[-1] < n:
self.queue.pop(-1)
else:
break
self.queue.append(n)
return
def is_empty(self):
return len(self.queue) == 0
def top(self):
return self.queue[0]
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_DEF WHILE VAR IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN VAR NUMBER
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_deque = collections.deque()
max_deque = collections.deque()
l = 0
ans = 0
for r, num in enumerate(nums):
while min_deque and nums[min_deque[-1]] >= num:
min_deque.pop()
while max_deque and nums[max_deque[-1]] <= num:
max_deque.pop()
min_deque.append(r)
max_deque.append(r)
while nums[max_deque[0]] - nums[min_deque[0]] > limit:
l += 1
while max_deque[0] < l:
max_deque.popleft()
while min_deque[0] < l:
min_deque.popleft()
ans = max(ans, r - l + 1)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
i, j = 0, 0
nums.append(float("inf"))
qmin = collections.deque()
qmax = collections.deque()
qmin.append(0)
qmax.append(0)
ans = 0
while j < len(nums) - 1:
while j < len(nums) - 1 and nums[qmax[0]] - nums[qmin[0]] <= limit:
ans = max(ans, j - i + 1)
j += 1
while qmax and nums[qmax[-1]] < nums[j]:
qmax.pop()
qmax.append(j)
while qmin and nums[qmin[-1]] > nums[j]:
qmin.pop()
qmin.append(j)
while qmax and nums[qmax[0]] - nums[j] > limit:
i = qmax.popleft() + 1
while qmin and nums[j] - nums[qmin[0]] > limit:
i = qmin.popleft() + 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_deque = collections.deque()
min_deque = collections.deque()
n = len(nums)
l = 0
res = 0
for r in range(n):
self.push_max_deque(max_deque, nums, r)
self.push_min_deque(min_deque, nums, r)
while nums[max_deque[0]] - nums[min_deque[0]] > limit:
if max_deque[0] == l:
max_deque.popleft()
if min_deque[0] == l:
min_deque.popleft()
l += 1
res = max(res, r - l + 1)
return res
def push_max_deque(self, max_deque, nums, i):
while max_deque and nums[max_deque[-1]] < nums[i]:
max_deque.pop()
max_deque.append(i)
def push_min_deque(self, min_deque, nums, i):
while min_deque and nums[min_deque[-1]] > nums[i]:
min_deque.pop()
min_deque.append(i)
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
answer = 1
counts = {nums[0]: 1}
subarray = deque([nums[0]])
max_val = nums[0]
min_val = nums[0]
for i in range(1, len(nums)):
counts[nums[i]] = counts.get(nums[i], 0) + 1
max_val = max(max_val, nums[i])
min_val = min(min_val, nums[i])
subarray.append(nums[i])
if abs(max_val - min_val) > limit:
while subarray:
num = subarray.popleft()
counts[num] -= 1
if counts[num] == 0:
if max_val == num:
if subarray:
max_val = max(subarray)
else:
max_val = 0
break
elif min_val == num:
if subarray:
min_val = min(subarray)
else:
min_val = 10**9 + 1
break
answer = max(answer, len(subarray))
return answer
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) == 0:
return 0
if len(nums) == 1:
return 1
maxStack = []
minStack = []
result = 1
leftPos = 0
for i in range(0, len(nums)):
while maxStack and nums[i] < nums[maxStack[0]] - limit:
leftPos = max(leftPos, maxStack.pop(0) + 1)
while minStack and nums[i] > nums[minStack[0]] + limit:
leftPos = max(leftPos, minStack.pop(0) + 1)
length = i - leftPos + 1
if length > result:
result = length
while maxStack and nums[i] > nums[maxStack[-1]]:
maxStack.pop()
maxStack.append(i)
while minStack and nums[i] < nums[minStack[-1]]:
minStack.pop()
minStack.append(i)
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums or len(nums) == 1:
return len(nums)
n = len(nums)
i = 0
j = 1
res = 1
min_in_window = nums[0]
max_in_window = nums[0]
while j < n:
max_in_window = max(max_in_window, nums[j])
min_in_window = min(min_in_window, nums[j])
diff = abs(min_in_window - max_in_window)
if diff <= limit:
j += 1
res = max(res, j - i)
else:
tmp = nums[i]
while i <= j and i < n - 1 and nums[i] == nums[i + 1]:
i += 1
if nums[i] == min_in_window:
min_in_window = min(nums[i + 1 : j + 1])
if nums[i] == max_in_window:
max_in_window = max(nums[i + 1 : j + 1])
i += 1
j += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
result = 0
min_deque = []
max_deque = []
start = 0
for index, value in enumerate(nums):
while len(min_deque) and value < min_deque[-1]:
min_deque.pop()
while len(max_deque) and value > max_deque[-1]:
max_deque.pop()
min_deque.append(value)
max_deque.append(value)
while max_deque[0] - min_deque[0] > limit:
if nums[start] == min_deque[0]:
min_deque.pop(0)
if nums[start] == max_deque[0]:
max_deque.pop(0)
start += 1
result = max(result, index - start + 1)
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
currMax = nums[0]
currMin = nums[0]
maxSize = 1
beg = 0
end = 0
while end < len(nums):
currMax = max(currMax, nums[end])
currMin = min(currMin, nums[end])
if currMax - currMin > limit:
beg += 1
if currMax == nums[beg - 1]:
while currMax == nums[beg]:
beg += 1
currMax = max(nums[beg : end + 1])
if currMin == nums[beg - 1]:
while currMin == nums[beg]:
beg += 1
currMin = min(nums[beg : end + 1])
else:
end += 1
maxSize = max(maxSize, end - beg)
return maxSize
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
res = 0
left, right = 0, 0
minimum, maximum = [], []
while right < n:
heapq.heappush(minimum, (nums[right], right))
heapq.heappush(maximum, (-nums[right], right))
right += 1
while -maximum[0][0] - minimum[0][0] > limit:
while minimum and minimum[0][1] <= left:
heapq.heappop(minimum)
while maximum and maximum[0][1] <= left:
heapq.heappop(maximum)
left += 1
res = max(res, right - left)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) == 0:
return 0
dec_stack = []
inc_stack = []
max_interval = 1
l, r = 0, 0
min_v, max_v = nums[0], nums[0]
dec_stack.append((nums[0], 0))
inc_stack.append((nums[0], 0))
while l < len(nums) and r < len(nums):
while max_v - min_v <= limit:
max_interval = max(max_interval, r - l + 1)
r += 1
if r == len(nums):
break
while dec_stack and nums[r] > dec_stack[-1][0]:
dec_stack.pop(-1)
dec_stack.append((nums[r], r))
max_v = dec_stack[0][0]
while inc_stack and nums[r] < inc_stack[-1][0]:
inc_stack.pop(-1)
inc_stack.append((nums[r], r))
min_v = inc_stack[0][0]
while max_v - min_v > limit:
if dec_stack[0][1] == l:
dec_stack.pop(0)
max_v = dec_stack[0][0]
if inc_stack[0][1] == l:
inc_stack.pop(0)
min_v = inc_stack[0][0]
l += 1
return max_interval
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
currmax = currmin = nums[0]
start = 0
res = 1
for i in range(1, len(nums)):
currmax = max(currmax, nums[i])
currmin = min(currmin, nums[i])
if currmax - currmin <= limit:
res = max(res, i - start + 1)
else:
if currmax == nums[start]:
currmax = max(nums[start + 1 : i + 1])
if currmin == nums[start]:
currmin = min(nums[start + 1 : i + 1])
start += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
p1, p2, ans, cnt, min_, max_ = 0, 0, 0, {nums[0]: 1}, nums[0], nums[0]
while True:
if max_ - min_ <= limit:
ans = max(ans, p2 - p1 + 1)
if p2 == len(nums) - 1:
return ans
p2 += 1
if nums[p2] in cnt:
cnt[nums[p2]] += 1
else:
cnt[nums[p2]] = 1
max_ = max(max_, nums[p2])
min_ = min(min_, nums[p2])
else:
num = nums[p1]
p1 += 1
if cnt[num] == 1:
del cnt[num]
if max_ == num:
max_ = max(cnt.keys())
if min_ == num:
min_ = min(cnt.keys())
else:
cnt[num] -= 1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER DICT VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
max_stack = collections.deque([0])
min_stack = collections.deque([0])
begin, end, longest = 0, 0, 0
while end < n:
while True:
diff = nums[max_stack[0]] - nums[min_stack[0]]
if diff > limit:
if min_stack[0] == begin:
min_stack.popleft()
if max_stack[0] == begin:
max_stack.popleft()
begin += 1
else:
longest = max(longest, end - begin + 1)
break
end += 1
if end < n:
while min_stack and nums[end] <= nums[min_stack[-1]]:
min_stack.pop()
min_stack.append(end)
while max_stack and nums[end] >= nums[max_stack[-1]]:
max_stack.pop()
max_stack.append(end)
return longest
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
mx, mi = [0], [0]
res = 1
left = 0
for c in range(1, len(nums)):
num = nums[c]
while mx and nums[mx[-1]] < num:
mx.pop()
mx.append(c)
while mi and nums[mi[-1]] > num:
mi.pop()
mi.append(c)
while mi and mx and nums[mx[0]] - nums[mi[0]] > limit:
left += 1
if left > mx[0]:
mx.pop(0)
if left > mi[0]:
mi.pop(0)
res = max(res, c - left + 1)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
incr_q = [nums[0]]
decr_q = [nums[0]]
res = 1
left = 0
right = 1
while right < len(nums):
while incr_q and incr_q[-1] > nums[right]:
incr_q.pop()
incr_q.append(nums[right])
while decr_q and decr_q[-1] < nums[right]:
decr_q.pop()
decr_q.append(nums[right])
while incr_q and decr_q and decr_q[0] - incr_q[0] > limit:
if nums[left] == incr_q[0]:
incr_q.pop(0)
if nums[left] == decr_q[0]:
decr_q.pop(0)
left += 1
res = max(right - left + 1, res)
right += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxheap, minheap = [(-nums[0], 0)], [(nums[0], 0)]
l, maxindex, res = 0, 0, 1
for i, val in enumerate(nums[1:], 1):
if val - minheap[0][0] <= limit and -maxheap[0][0] - val <= limit:
res = max(res, i - maxindex + 1)
else:
while minheap and val - minheap[0][0] > limit:
v, index = heapq.heappop(minheap)
maxindex = max(maxindex, index + 1)
while maxheap and -maxheap[0][0] - val > limit:
v, index = heapq.heappop(maxheap)
maxindex = max(maxindex, index + 1)
heapq.heappush(minheap, (val, i))
heapq.heappush(maxheap, (-val, i))
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST VAR NUMBER NUMBER LIST VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
res = 0
left, right = 0, 0
inc, dec = collections.deque(), collections.deque()
while right < n:
while inc and nums[inc[-1]] >= nums[right]:
inc.pop()
inc.append(right)
while dec and nums[dec[-1]] <= nums[right]:
dec.pop()
dec.append(right)
right += 1
while nums[dec[0]] - nums[inc[0]] > limit:
if inc[0] == left:
inc.popleft()
if dec[0] == left:
dec.popleft()
left += 1
res = max(res, right - left)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
max_q = deque()
min_q = deque()
start = 0
ans = 0
for end in range(n):
while max_q and max_q[-1] < nums[end]:
max_q.pop()
while min_q and min_q[-1] > nums[end]:
min_q.pop()
max_q.append(nums[end])
min_q.append(nums[end])
if max_q[0] - min_q[0] > limit:
if max_q[0] == nums[start]:
max_q.popleft()
if min_q[0] == nums[start]:
min_q.popleft()
start += 1
ans = max(end - start + 1, ans)
return ans
def longestSubarray2(self, nums: List[int], limit: int) -> int:
q = deque()
min_q = deque()
max_q = deque()
ans = 0
for num in nums:
q.append(num)
while min_q and min_q[-1] > num:
min_q.pop()
min_q.append(num)
while max_q and max_q[-1] < num:
max_q.pop()
max_q.append(num)
while q and max_q[0] - min_q[0] > limit:
del_num = q.popleft()
if max_q[0] == del_num:
max_q.popleft()
if min_q[0] == del_num:
min_q.popleft()
ans = max(ans, len(q))
return ans
def longestSubarray2(self, nums: List[int], limit: int) -> int:
min_q = deque()
max_q = deque()
start = 0
for num in nums:
while min_q and min_q[-1] > num:
min_q.pop()
min_q.append(num)
while max_q and max_q[-1] < num:
max_q.pop()
max_q.append(num)
if max_q[0] - min_q[0] > limit:
if max_q[0] == nums[start]:
max_q.popleft()
if min_q[0] == nums[start]:
min_q.popleft()
start += 1
return len(nums) - start
def longestSubarray2(self, nums: List[int], limit: int) -> int:
n = len(nums)
min_dp = [[(0) for _ in range(n)] for _ in range(n)]
max_dp = [[(0) for _ in range(n)] for _ in range(n)]
ans = 1 if 0 <= limit else 0
for i in range(n):
min_dp[i][i] = nums[i]
max_dp[i][i] = nums[i]
for k in range(2, n + 1):
for i in range(n - k + 1):
j = i + k - 1
min_dp[i][j] = min(min_dp[i][j - 1], nums[j])
max_dp[i][j] = max(max_dp[i][j - 1], nums[j])
if max_dp[i][j] - min_dp[i][j] <= limit:
ans = k
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) == 0:
return 0
if len(nums) == 1:
return 1
ans = 1
minNum = nums[0]
maxNum = nums[0]
i = 0
j = 0
n = len(nums)
while i < n and j < n:
maxNum = max(maxNum, nums[i])
minNum = min(minNum, nums[i])
if abs(maxNum - minNum) > limit:
if nums[j] == maxNum:
maxNum = max(nums[j + 1 : i + 1])
elif nums[j] == minNum:
minNum = min(nums[j + 1 : i + 1])
j += 1
else:
ans = max(ans, i - j + 1)
i += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
n = len(nums)
i = j = 0
curr_max = curr_min = nums[0]
maxes = [(-curr_max, 0)]
mins = [(curr_min, 0)]
best = 0
while i <= j < n:
if abs(-maxes[0][0] - mins[0][0]) <= limit:
best = max(best, j - i + 1)
j += 1
if j < n:
heapq.heappush(maxes, (-nums[j], j))
heapq.heappush(mins, (nums[j], j))
else:
i += 1
while not i <= maxes[0][1] <= j:
heapq.heappop(maxes)
while not i <= mins[0][1] <= j:
heapq.heappop(mins)
return best
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, A, limit):
M = collections.deque()
m = collections.deque()
i = 0
for a in A:
while M and a > M[-1]:
M.pop()
while m and a < m[-1]:
m.pop()
M.append(a)
m.append(a)
if M[0] - m[0] > limit:
if M[0] == A[i]:
M.popleft()
if m[0] == A[i]:
m.popleft()
i += 1
return len(A) - i
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_deque = collections.deque()
min_deque = collections.deque()
start = 0
end = 0
max_limit_subarray = 0
for end in range(len(nums)):
while min_deque and nums[end] < nums[min_deque[-1]]:
min_deque.pop()
min_deque.append(end)
while max_deque and nums[end] > nums[max_deque[-1]]:
max_deque.pop()
max_deque.append(end)
while abs(nums[max_deque[0]] - nums[min_deque[0]]) > limit:
start += 1
if start > min_deque[0]:
min_deque.popleft()
if start > max_deque[0]:
max_deque.popleft()
max_limit_subarray = max(max_limit_subarray, end - start + 1)
return max_limit_subarray
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxd, mind = collections.deque(), collections.deque()
l, r, res = 0, 0, 0
while r < len(nums):
while len(maxd) and maxd[-1] < nums[r]:
maxd.pop()
while len(mind) and mind[-1] > nums[r]:
mind.pop()
maxd.append(nums[r])
mind.append(nums[r])
while maxd[0] - mind[0] > limit:
if nums[l] == maxd[0]:
maxd.popleft()
if nums[l] == mind[0]:
mind.popleft()
l += 1
res = max(r - l + 1, res)
r += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_deque, max_deque = deque(), deque()
l = r = 0
ans = 0
while r < len(nums):
while min_deque and nums[r] <= nums[min_deque[-1]]:
min_deque.pop()
while max_deque and nums[r] >= nums[max_deque[-1]]:
max_deque.pop()
min_deque.append(r)
max_deque.append(r)
if nums[max_deque[0]] - nums[min_deque[0]] <= limit:
ans = max(ans, r - l + 1)
r += 1
elif nums[max_deque[0]] - nums[min_deque[0]] > limit:
while min_deque[0] <= l:
min_deque.popleft()
while max_deque[0] <= l:
max_deque.popleft()
l += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
i = 0
j = 0
n = len(nums)
maxq = []
minq = []
max_l = 0
while i < n and j < n:
while maxq and nums[j] > maxq[-1]:
maxq.pop()
while minq and nums[j] < minq[-1]:
minq.pop()
maxq.append(nums[j])
minq.append(nums[j])
while maxq[0] - minq[0] > limit:
if nums[i] == maxq[0]:
maxq.pop(0)
if nums[i] == minq[0]:
minq.pop(0)
i += 1
max_l = max(max_l, j - i + 1)
j += 1
return max_l
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) <= 1:
return len(nums)
queue = [nums[0]]
small = queue[0]
large = queue[0]
ans = 1
for i in range(1, len(nums)):
if abs(nums[i] - small) <= limit and abs(nums[i] - large) <= limit:
queue.append(nums[i])
small = min(small, nums[i])
large = max(large, nums[i])
ans = max(ans, len(queue))
else:
if ans > len(nums) // 2:
break
while queue and not (
abs(nums[i] - small) <= limit and abs(nums[i] - large) <= limit
):
queue.pop(0)
if queue:
small = min(queue)
large = max(queue)
else:
break
if queue:
queue.append(nums[i])
small = min(queue)
large = max(queue)
else:
queue = [nums[i]]
small = nums[i]
large = nums[i]
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxque = collections.deque([])
minque = collections.deque([])
start = end = 0
count = 0
while end < len(nums):
while len(maxque) and nums[end] > nums[maxque[-1]]:
maxque.pop()
while len(minque) and nums[end] < nums[minque[-1]]:
minque.pop()
maxque.append(end)
minque.append(end)
if nums[maxque[0]] - nums[minque[0]] > limit:
if maxque[0] == start:
maxque.popleft()
if minque[0] == start:
minque.popleft()
start += 1
end += 1
else:
end += 1
return len(nums) - start
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class MinMaxStack:
def __init__(self):
self.s = []
self.mins = []
self.maxes = []
def append(self, x):
self.s.append(x)
if not self.mins or x <= self.mins[-1]:
self.mins.append(x)
if not self.maxes or x >= self.maxes[-1]:
self.maxes.append(x)
def pop(self):
x = self.s.pop()
if x == self.mins[-1]:
self.mins.pop()
if x == self.maxes[-1]:
self.maxes.pop()
return x
def min(self):
return self.mins[-1] if self.mins else float("inf")
def max(self):
return self.maxes[-1] if self.maxes else -float("inf")
def __len__(self):
return len(self.s)
class MinMaxQueue:
def __init__(self):
self.s1 = MinMaxStack()
self.s2 = MinMaxStack()
def enqueue(self, x):
self.s1.append(x)
def dequeue(self):
if not self.s2:
while self.s1:
to_append = self.s1.pop()
self.s2.append(to_append)
return self.s2.pop()
def min(self):
return min(self.s1.min(), self.s2.min())
def max(self):
return max(self.s1.max(), self.s2.max())
def __len__(self):
return len(self.s1) + len(self.s2)
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
i = 0
max_len = 1
q = MinMaxQueue()
q.enqueue(nums[0])
while i < len(nums):
if q.max() - q.min() <= limit:
max_len = max(max_len, len(q))
i += 1
if i < len(nums):
q.enqueue(nums[i])
continue
q.dequeue()
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF RETURN VAR VAR NUMBER FUNC_CALL VAR STRING FUNC_DEF RETURN VAR VAR NUMBER FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxq = collections.deque()
minq = collections.deque()
start = 0
for i, n in enumerate(nums):
while maxq and maxq[-1][0] < n:
maxq.pop()
while minq and minq[-1][0] > n:
minq.pop()
maxq.append([n, i])
minq.append([n, i])
if maxq[0][0] - minq[0][0] > limit:
if maxq[0][0] == nums[start]:
maxq.popleft()
if minq[0][0] == nums[start]:
minq.popleft()
start += 1
return len(nums) - start
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxD = []
minD = []
ind = 0
res = 0
slow = 0
fast = 0
while slow <= fast and fast < len(nums):
while len(maxD) > 0 and maxD[len(maxD) - 1] < nums[fast]:
maxD.pop(len(maxD) - 1)
maxD.append(nums[fast])
while len(minD) > 0 and minD[len(minD) - 1] > nums[fast]:
minD.pop(len(minD) - 1)
minD.append(nums[fast])
while maxD[0] - minD[0] > limit and slow < fast:
if maxD[0] == nums[slow]:
maxD.pop(0)
if minD[0] == nums[slow]:
minD.pop(0)
slow += 1
res = max(res, fast - slow + 1)
fast += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
def add_to_max_heap(heap, value):
heapq.heappush(heap, value)
def add_to_min_heap(heap, value):
heapq.heappush(heap, value)
def get_max_val(heap):
return heap[0][0]
def get_min_val(heap):
return heap[0][0]
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
l = 0
r = 0
ans = 0
max_heap = []
min_heap = []
while l < len(nums) and r < len(nums):
add_to_max_heap(max_heap, (-nums[r], r))
add_to_min_heap(min_heap, (nums[r], r))
max_val = -get_max_val(max_heap)
min_val = get_min_val(min_heap)
if abs(max_val - min_val) <= limit:
ans = max(ans, r - l + 1)
r += 1
else:
while min_heap and min_heap[0][1] < l + 1:
heapq.heappop(min_heap)
while max_heap and max_heap[0][1] < l + 1:
heapq.heappop(max_heap)
l += 1
return ans
|
FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN VAR NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) < 2:
return 1
longestSize = 1
left = 0
lo_q, hi_q = [], []
heapq.heappush(lo_q, (nums[0], 0))
heapq.heappush(hi_q, (-nums[0], 0))
for idx in range(1, len(nums)):
if lo_q and nums[idx] < lo_q[0][0]:
lo_q = []
elif hi_q and -nums[idx] < hi_q[0][0]:
hi_q = []
heapq.heappush(lo_q, (nums[idx], idx))
heapq.heappush(hi_q, (-nums[idx], idx))
while lo_q and abs(nums[idx] - lo_q[0][0]) > limit:
left = max(left, heapq.heappop(lo_q)[1] + 1)
while hi_q and abs(nums[idx] + hi_q[0][0]) > limit:
left = max(left, heapq.heappop(hi_q)[1] + 1)
longestSize = max(longestSize, idx - left + 1)
return longestSize
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
l = len(nums)
if l == 1:
if limit >= 0:
return 1
return 0
max_count = i = 0
j = 1
def get_min():
while min_list[0][1] < i:
heappop(min_list)
return min_list[0][1]
def get_max():
while max_list[0][1] < i:
heappop(max_list)
return max_list[0][1]
min_list = []
max_list = []
heappush(min_list, (nums[0], 0))
heappush(max_list, (-nums[0], 0))
while l > j >= i:
heappush(min_list, (nums[j], j))
heappush(max_list, (-nums[j], j))
mn = get_min()
mx = get_max()
diff = nums[mx] - nums[mn]
if diff > limit:
if j != i + 1:
max_count = max(max_count, j - i)
i = min(mn, mx) + 1
else:
j += 1
i += 1
else:
j += 1
if j == l and i < j - 1:
max_count = max(max_count, j - i)
return max_count
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
start = 0
end = 0
longest_size = 0
maxa = nums[0]
mina = nums[0]
while end < len(nums):
if maxa - mina <= limit:
longest_size = max(longest_size, end - start)
maxa = max(maxa, nums[end])
mina = min(mina, nums[end])
end += 1
else:
remove = nums[start]
start += 1
if remove == mina:
while nums[start] <= mina:
start += 1
mina = min(nums[start : end + 1])
if remove == maxa:
while nums[start] >= maxa:
start += 1
maxa = max(nums[start : end + 1])
if maxa - mina <= limit:
longest_size = max(longest_size, end - start)
return longest_size
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
winMax = [0]
winMin = [0]
i = j = 0
res = 0
while i < len(nums) - 1 and j < len(nums) - 1:
diff = nums[winMax[0]] - nums[winMin[0]]
if diff <= limit:
res = max(res, j - i + 1)
if diff <= limit:
j += 1
while winMax and nums[j] > nums[winMax[-1]]:
winMax.pop(-1)
winMax.append(j)
while winMin and nums[j] < nums[winMin[-1]]:
winMin.pop(-1)
winMin.append(j)
else:
if i == winMax[0]:
winMax.pop(0)
if i == winMin[0]:
winMin.pop(0)
i += 1
diff = nums[winMax[0]] - nums[winMin[0]]
if diff <= limit:
res = max(res, j - i + 1)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
begin = end = 0
l = len(nums)
d = 0
heap_min = []
heap_max = []
heapq.heapify(heap_min)
heapq.heapify(heap_max)
while end < l:
heapq.heappush(heap_min, (nums[end], end))
heapq.heappush(heap_max, (-1 * nums[end], end))
while len(heap_min) > 0 and nums[end] - heap_min[0][0] > limit:
value, idx = heapq.heappop(heap_min)
begin = max(begin, idx + 1)
while len(heap_max) > 0 and -heap_max[0][0] - nums[end] > limit:
value, idx = heapq.heappop(heap_max)
begin = max(begin, idx + 1)
if end - begin + 1 > d:
d = end - begin + 1
end += 1
return d
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if limit < 0:
return 0
i, j = 0, 1
res = 0
max_q, min_q = collections.deque(), collections.deque()
max_q.append((0, nums[0]))
min_q.append((0, nums[0]))
while j < len(nums):
if max_q[0][1] - min_q[0][1] <= limit:
while max_q and nums[j] > max_q[-1][1]:
max_q.pop()
while min_q and nums[j] < min_q[-1][1]:
min_q.pop()
max_q.append((j, nums[j]))
min_q.append((j, nums[j]))
j += 1
if max_q[0][1] - min_q[0][1] > limit:
res = j - i - 1 if j - i - 1 > res else res
min_i, max_i = min_q[0][0], max_q[0][0]
if min_i < max_i:
i = min_i + 1
min_q.popleft()
else:
i = max_i + 1
max_q.popleft()
return j - i if j - i > res else res
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR WHILE VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
minheap = []
maxheap = []
heapq.heapify(minheap)
heapq.heapify(maxheap)
length = 1
i = 0
j = 1
heapq.heappush(minheap, [nums[0], 0])
heapq.heappush(maxheap, [-nums[0], 0])
while j < len(nums):
if (
abs(minheap[0][0] - nums[j]) <= limit
and abs(abs(maxheap[0][0]) - nums[j]) <= limit
):
length = max(length, j - i + 1)
heapq.heappush(minheap, [nums[j], j])
heapq.heappush(maxheap, [-nums[j], j])
j = j + 1
else:
while len(minheap) > 0 and abs(minheap[0][0] - nums[j]) > limit:
ele, index = heapq.heappop(minheap)
i = max(i, index + 1)
while len(maxheap) > 0 and abs(-maxheap[0][0] - nums[j]) > limit:
ele, index = heapq.heappop(maxheap)
i = max(i, index + 1)
heapq.heappush(minheap, [nums[j], j])
heapq.heappush(maxheap, [-nums[j], j])
j = j + 1
return length
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR LIST VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
l = ans = 0
maxh, minh = [], []
for r, num in enumerate(nums):
heapq.heappush(maxh, (-1 * num, r))
heapq.heappush(minh, (num, r))
while maxh[0][0] * -1 - minh[0][0] > limit:
while maxh[0][1] <= l:
heapq.heappop(maxh)
while minh[0][1] <= l:
heapq.heappop(minh)
l += 1
ans = max(ans, r - l + 1)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
maxs, mins = [], []
res = 0
start = 0
for i, n in enumerate(nums):
while maxs and maxs[-1] < n:
maxs.pop()
while mins and mins[-1] > n:
mins.pop()
maxs.append(n)
mins.append(n)
while maxs[0] - mins[0] > limit:
if maxs[0] == nums[start]:
maxs.pop(0)
if mins[0] == nums[start]:
mins.pop(0)
start += 1
res = max(res, i - start + 1)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) == 1 and limit >= 0:
return 1
longest_subarray = 0
right = 0
left = 0
min_val = nums[left]
max_val = nums[left]
min_dq = []
max_dq = []
while right < len(nums):
while len(min_dq) > 0 and min_dq[-1] > nums[right]:
min_dq.pop()
min_dq.append(nums[right])
while len(max_dq) > 0 and max_dq[-1] < nums[right]:
max_dq.pop()
max_dq.append(nums[right])
min_val = min_dq[0]
max_val = max_dq[0]
if abs(min_val - max_val) > limit:
if min_dq[0] == nums[left]:
min_dq.pop(0)
if max_dq[0] == nums[left]:
max_dq.pop(0)
left += 1
longest_subarray = max(longest_subarray, right - left + 1)
right += 1
return longest_subarray
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
curr_max = nums[0]
curr_min = nums[0]
sub_nums = []
for num in nums:
if (
abs(num - curr_max) <= limit
and abs(num - curr_min) <= limit
and abs(curr_max - curr_min) <= limit
):
curr_max = max(num, curr_max)
curr_min = min(num, curr_min)
sub_nums.append(num)
else:
sub_nums.append(num)
sub_nums.pop(0)
curr_max = max(sub_nums)
curr_min = min(sub_nums)
return len(sub_nums)
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_heap = []
max_heap = []
longest_window = 1
min_idx = 0
for i, num in enumerate(nums):
heapq.heappush(min_heap, (num, i))
heapq.heappush(max_heap, (-num, i))
while min_heap[0][1] < min_idx:
heapq.heappop(min_heap)
while max_heap[0][1] < min_idx:
heapq.heappop(max_heap)
if abs(min_heap[0][0] + max_heap[0][0]) > limit:
if min_heap[0][1] < max_heap[0][1]:
min_idx = min_heap[0][1] + 1
heapq.heappop(min_heap)
else:
min_idx = max_heap[0][1] + 1
heapq.heappop(max_heap)
longest_window = max(longest_window, i - min_idx + 1)
return longest_window
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
i = 0
j = 0
max_length = 1
cur_max = cur_min = nums[0]
while j < len(nums):
dif = cur_max - cur_min
if dif <= limit:
max_length = j - i + 1
j += 1
if j < len(nums):
cur_max = max(cur_max, nums[j])
cur_min = min(cur_min, nums[j])
else:
i += 1
j += 1
cur_max = max(nums[i : j + 1])
cur_min = min(nums[i : j + 1])
return max_length
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_queue = collections.deque()
min_queue = collections.deque()
l = r = 0
max_len = 0
while r < len(nums):
while max_queue and nums[r] >= max_queue[-1][0]:
max_queue.pop()
max_queue.append((nums[r], r))
while min_queue and nums[r] <= min_queue[-1][0]:
min_queue.pop()
min_queue.append((nums[r], r))
r += 1
while max_queue[0][0] - min_queue[0][0] > limit:
l += 1
if max_queue[0][1] < l:
max_queue.popleft()
if min_queue[0][1] < l:
min_queue.popleft()
max_len = max(max_len, r - l)
return max_len
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
i, j, ans = 0, 0, 0
min_heap, max_heap = [], []
heapq.heapify(min_heap)
heapq.heapify(max_heap)
while i <= j < n:
heapq.heappush(min_heap, (nums[j], j))
heapq.heappush(max_heap, (-nums[j], j))
while i <= j < n and -max_heap[0][0] - min_heap[0][0] > limit:
i += 1
while min_heap and min_heap[0][1] < i:
heapq.heappop(min_heap)
while max_heap and max_heap[0][1] < i:
heapq.heappop(max_heap)
ans = max(ans, j - i + 1)
j += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
VALUE, INDEX = 0, 1
left = 0
min_h, max_h = [], []
max_len = 0
for right, num in enumerate(nums):
heappush(min_h, (num, right))
heappush(max_h, (-num, right))
while -max_h[0][VALUE] - min_h[0][VALUE] > limit:
if max_h[0][INDEX] < min_h[0][INDEX]:
left = heappop(max_h)[INDEX] + 1
else:
left = heappop(min_h)[INDEX] + 1
while min_h[0][INDEX] < left:
heappop(min_h)
while max_h[0][INDEX] < left:
heappop(max_h)
max_len = max(max_len, right - left + 1)
return max_len
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
ans = 1
st = en = 0
min_q = collections.deque()
max_q = collections.deque()
while en < len(nums):
while min_q and nums[en] <= nums[min_q[-1]]:
min_q.pop()
while max_q and nums[en] >= nums[max_q[-1]]:
max_q.pop()
min_q.append(en)
max_q.append(en)
while nums[max_q[0]] - nums[min_q[0]] > limit:
st += 1
if st > min_q[0]:
min_q.popleft()
if st > max_q[0]:
max_q.popleft()
ans = max(ans, en - st + 1)
en += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
cur_max = nums[0]
cur_min = nums[0]
sub_nums = []
for i in nums:
cur_max = max(i, cur_max)
cur_min = min(i, cur_min)
if cur_max - cur_min <= limit:
sub_nums.append(i)
else:
sub_nums.append(i)
sub_nums.pop(0)
cur_max = max(sub_nums)
cur_min = min(sub_nums)
return len(sub_nums)
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
count = collections.defaultdict(int)
min_heap = []
max_heap = []
ans = []
max_length = 0
for num in nums:
ans.append(num)
count[num] += 1
if count[num] == 1:
heapq.heappush(min_heap, num)
heapq.heappush(max_heap, num * -1)
max_val = abs(max_heap[0])
min_val = min_heap[0]
while max_val - min_val > limit:
popped_val = ans.pop(0)
count[popped_val] -= 1
if popped_val == min_val and count[popped_val] == 0:
while count[min_val] == 0:
heapq.heappop(min_heap)
min_val = min_heap[0]
if popped_val == max_val and count[popped_val] == 0:
while count[max_val] == 0:
heapq.heappop(max_heap)
max_val = abs(max_heap[0])
max_length = max(max_length, len(ans))
return max_length
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_list = []
min_list = []
j = 0
for i in range(len(nums)):
while max_list and max_list[-1] < nums[i]:
max_list.pop()
while min_list and min_list[-1] > nums[i]:
min_list.pop()
max_list.append(nums[i])
min_list.append(nums[i])
if max_list[0] - min_list[0] > limit:
if max_list[0] == nums[j]:
max_list = max_list[1:]
if min_list[0] == nums[j]:
min_list = min_list[1:]
j += 1
return len(nums) - j
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
hmin, hmax = [], []
heapq.heapify(hmin), heapq.heapify(hmax)
max_length = 0
i, j = 0, 0
heapq.heappush(hmin, (nums[0], 0)), heapq.heappush(hmax, (-1 * nums[0], 0))
while j < len(nums):
while hmax[0][1] < i:
heapq.heappop(hmax)
while hmin[0][1] < i:
heapq.heappop(hmin)
if -1 * (hmax[0][0] + hmin[0][0]) <= limit:
max_length = max(max_length, j - i + 1)
j += 1
if j >= len(nums):
break
heapq.heappush(hmin, (nums[j], j))
heapq.heappush(hmax, (-1 * nums[j], j))
else:
i += 1
return max_length
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
minHeap = []
maxHeap = []
l = 0
maxSize = 0
for r, num in enumerate(nums):
heappush(minHeap, (num, r))
heappush(maxHeap, (-num, r))
minElement, minIndex = minHeap[0]
maxElement, maxIndex = maxHeap[0]
maxElement = -maxElement
while maxElement - minElement > limit:
l += 1
while minHeap[0][1] < l:
heappop(minHeap)
while maxHeap[0][1] < l:
heappop(maxHeap)
minElement, minIndex = minHeap[0]
maxElement, maxIndex = maxHeap[0]
maxElement = -maxElement
curSize = r - l + 1
maxSize = max(curSize, maxSize)
return maxSize
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
minHeap, maxHeap = [], []
left = maxLen = 0
for right in range(len(nums)):
heappush(minHeap, (nums[right], right))
heappush(maxHeap, (-nums[right], right))
while nums[right] - minHeap[0][0] > limit:
left = max(left, heappop(minHeap)[1] + 1)
while -maxHeap[0][0] - nums[right] > limit:
left = max(left, heappop(maxHeap)[1] + 1)
maxLen = max(maxLen, right - left + 1)
return maxLen
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
result = 1
start = 0
end = start + result
mini = float("inf")
maxi = float("-inf")
while end <= len(nums):
mini = min(nums[start:end])
maxi = max(nums[start:end])
if abs(mini - maxi) > limit:
start += 1
end += 1
else:
end += 1
while end <= len(nums):
mini = min(nums[end - 1], mini)
maxi = max(nums[end - 1], maxi)
if abs(mini - maxi) > limit:
result = max(result, end - start - 1)
start += 1
end = start + result
break
else:
end += 1
if end > len(nums):
result = max(result, end - start - 1)
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
def addToDeqs(min_deq, max_deq, curr, start_i):
while min_deq and min_deq[-1] > curr:
min_deq.pop()
min_deq.append(curr)
while max_deq and max_deq[-1] < curr:
max_deq.pop()
max_deq.append(curr)
while abs(max_deq[0] - min_deq[0]) > limit:
if max_deq[0] == nums[start_i]:
max_deq.pop(0)
if min_deq[0] == nums[start_i]:
min_deq.pop(0)
start_i += 1
return start_i
min_deq = []
max_deq = []
longest_subarray = 0
start_i = 0
for i, n in enumerate(nums):
start_i = addToDeqs(min_deq, max_deq, n, start_i)
length_subarray = i - start_i + 1
longest_subarray = max(longest_subarray, length_subarray)
return longest_subarray
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if not nums:
return 0
rlt = 1
q = collections.deque([nums[0]])
max_stack, min_stack = [nums[0]], [nums[0]]
for i in range(1, len(nums)):
while max_stack and (
max_stack[0] - nums[i] > limit or nums[i] - min_stack[0] > limit
):
head = q.popleft()
if head == max_stack[0]:
max_stack.pop(0)
if head == min_stack[0]:
min_stack.pop(0)
q.append(nums[i])
while max_stack and max_stack[-1] < nums[i]:
max_stack.pop()
while min_stack and min_stack[-1] > nums[i]:
min_stack.pop()
max_stack.append(nums[i])
min_stack.append(nums[i])
rlt = max(rlt, len(q))
return rlt
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR LIST VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
sd = collections.deque()
ld = collections.deque()
def smallpush(d, i):
while d and d[-1][0] > nums[i]:
d.pop()
d.append((nums[i], i))
def largepush(d, i):
while d and d[-1][0] < nums[i]:
d.pop()
d.append((nums[i], i))
def popleft(d, j):
while d and d[0][1] < j:
d.popleft()
n = len(nums)
ans = -math.inf
start = 0
i = 0
while i < n:
smallpush(sd, i)
largepush(ld, i)
if abs(ld[0][0] - sd[0][0]) <= limit:
ans = max(ans, i - start + 1)
else:
while abs(ld[0][0] - sd[0][0]) > limit:
if ld[0][1] == start:
ld.popleft()
if sd[0][1] == start:
sd.popleft()
start += 1
ans = max(ans, i - start + 1)
i += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF WHILE VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF WHILE VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if len(nums) == 1:
return 1
left = right = 0
min_n = max_n = nums[0]
max_size = 0
while right < len(nums):
if nums[right] > max_n:
max_n = nums[right]
if nums[right] < min_n:
min_n = nums[right]
while max_n - min_n > limit:
left += 1
min_n = min(nums[left : right + 1])
max_n = max(nums[left : right + 1])
break
if right - left > max_size:
max_size = right - left
right += 1
return max_size + 1
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_heap, max_heap = deque([0]), deque([0])
res = 1
far = -1
for i in range(1, len(nums)):
while min_heap and nums[min_heap[-1]] > nums[i]:
min_heap.pop()
while max_heap and nums[max_heap[-1]] < nums[i]:
max_heap.pop()
while min_heap and abs(nums[min_heap[0]] - nums[i]) > limit:
far = max(far, min_heap.popleft())
while max_heap and abs(nums[max_heap[0]] - nums[i]) > limit:
far = max(far, max_heap.popleft())
res = max(res, i - far)
min_heap.append(i)
max_heap.append(i)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST NUMBER FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
max_len = 1
maxi = nums[0]
mini = nums[0]
start = 0
temp = nums[start : start + 1]
for i in range(1, len(nums)):
temp.append(nums[i])
if nums[i] > maxi:
maxi = nums[i]
if nums[i] < mini:
mini = nums[i]
if maxi - mini <= limit:
max_len = max(max_len, len(temp))
else:
temp.pop(start)
maxi = max(temp)
mini = min(temp)
return max_len
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
i = 0
j = 1
n = len(nums)
if n == 0:
return 0
mn = nums[0]
mx = nums[0]
l = 1
while i <= j and j < n:
if mn > nums[j]:
mn = nums[j]
if mx < nums[j]:
mx = nums[j]
if mx - mn <= limit:
if j - i + 1 > l:
l = j - i + 1
else:
if mn == nums[i]:
mn = min(nums[i + 1 : j + 1])
if mx == nums[i]:
mx = max(nums[i + 1 : j + 1])
i += 1
j += 1
return l
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
begin, end = 0, 0
n = len(nums)
heap_min = []
heapq.heapify(heap_min)
heap_max = []
heapq.heapify(heap_max)
heapq.heappush(heap_min, (nums[0], 0))
heapq.heappush(heap_max, (-nums[0], 0))
max_length = 1
while end < n - 1:
end += 1
if (
abs(nums[end] - heap_min[0][0]) > limit
or abs(-heap_max[0][0] - nums[end]) > limit
):
while len(heap_min) > 0 and nums[end] - heap_min[0][0] > limit:
value, idx = heapq.heappop(heap_min)
begin = max(begin, idx + 1)
while len(heap_max) > 0 and -heap_max[0][0] - nums[end] > limit:
value, idx = heapq.heappop(heap_max)
begin = max(begin, idx + 1)
heapq.heappush(heap_min, (nums[end], end))
heapq.heappush(heap_max, (-nums[end], end))
max_length = max(max_length, end - begin + 1)
return max_length
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums, limit):
_min = _max = nums[0]
count = 0
res, start = 1, 0
for right in range(len(nums)):
if _min > nums[right]:
_min = nums[right]
if _max < nums[right]:
_max = nums[right]
if _max - _min <= limit:
count += 1
if res < count:
res = count
else:
_min = _max = nums[right]
count = 1
left = right - 1
while (
left >= start
and abs(_max - nums[left]) <= limit
and abs(_min - nums[left]) <= limit
):
if _min > nums[left]:
_min = nums[left]
if _max < nums[left]:
_max = nums[left]
count += 1
left -= 1
start = right + 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
mxh, mnh = [], []
i = j = 0
ans = 0
while i < len(nums):
if j < len(nums) and (
not mxh or not mnh or abs(mxh[0][0] + mnh[0][0]) <= limit
):
heappush(mnh, [nums[j], j])
heappush(mxh, [-nums[j], j])
j += 1
else:
if not mxh or not mnh or abs(mxh[0][0] + mnh[0][0]) <= limit:
ans = max(ans, j - i)
else:
ans = max(ans, j - i - 1)
i += 1
while mxh and mxh[0][1] < i:
heappop(mxh)
while mnh and mnh[0][1] < i:
heappop(mnh)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_h = [(nums[0], 0)]
max_h = [(-nums[0], 0)]
heapq.heapify(min_h)
heapq.heapify(max_h)
max_v, min_v = nums[0], nums[0]
l, r = 0, 0
res = 1
while r < len(nums):
if max_v - min_v <= limit:
r += 1
if r < len(nums):
heapq.heappush(min_h, (nums[r], r))
heapq.heappush(max_h, (-nums[r], r))
max_v, min_v = -max_h[0][0], min_h[0][0]
else:
res = max(res, r - l)
l += 1
while min_h[0][1] < l:
heapq.heappop(min_h)
while max_h[0][1] < l:
heapq.heappop(max_h)
max_v, min_v = -max_h[0][0], min_h[0][0]
return max(res, r - l)
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
min_deque = [nums[0]]
max_deque = [nums[0]]
max_stretch = 1
start = 0
end = 0
while end < len(nums) - 1:
end += 1
while len(min_deque) > 0 and nums[end] < min_deque[-1]:
min_deque.pop()
min_deque.append(nums[end])
while len(max_deque) > 0 and nums[end] > max_deque[-1]:
max_deque.pop()
max_deque.append(nums[end])
while max_deque[0] - min_deque[0] > limit:
if min_deque[0] == nums[start]:
min_deque.pop(0)
if max_deque[0] == nums[start]:
max_deque.pop(0)
start += 1
if end - start + 1 > max_stretch:
max_stretch = end - start + 1
return max_stretch
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums, limit):
mini, maxi, res, i = deque([]), deque([]), 0, 0
for j in range(len(nums)):
while mini and mini[-1][0] > nums[j]:
mini.pop()
mini.append((nums[j], j))
while maxi and maxi[-1][0] < nums[j]:
maxi.pop()
maxi.append((nums[j], j))
while abs(maxi[0][0] - mini[0][0]) > limit and i < j:
if maxi[0][-1] <= i:
maxi.popleft()
elif mini[0][-1] <= i:
mini.popleft()
i += 1
res = max(res, j - i + 1)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR LIST FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
if nums is None or len(nums) == 0:
return 0
queue = collections.deque([])
max_len = 0
right = 0
min_heap = []
max_heap = []
for left in range(len(nums)):
if left == right:
heapq.heappush(min_heap, (nums[left], left))
heapq.heappush(max_heap, (-nums[left], left))
right += 1
while right < len(nums):
cur_min = min_heap[0][0]
cur_max = -max_heap[0][0]
if abs(nums[right] - cur_min) > limit:
break
if abs(nums[right] - cur_max) > limit:
break
heapq.heappush(min_heap, (nums[right], right))
heapq.heappush(max_heap, (-nums[right], right))
right += 1
max_len = max(max_len, right - left)
if right == len(nums):
return max_len
while max_heap and max_heap[0][1] <= left:
heapq.heappop(max_heap)
while min_heap and min_heap[0][1] <= left:
heapq.heappop(min_heap)
return max_len
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given an array of integers nums and an integer limit, 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 to limit.
Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[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:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= limit <= 10^9
|
class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
n = len(nums)
minh = []
maxh = []
start = 0
ans = 0
deleted = set()
for end in range(n):
x = nums[end]
heappush(minh, (x, end))
heappush(maxh, (-x, end))
while minh and minh[0][1] in deleted:
heappop(minh)
while maxh and maxh[0][1] in deleted:
heappop(maxh)
while minh and maxh and -maxh[0][0] - minh[0][0] > limit and start <= end:
deleted.add(start)
start += 1
while minh and minh[0][1] in deleted:
heappop(minh)
while maxh and maxh[0][1] in deleted:
heappop(maxh)
ans = max(ans, end - start + 1)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
|
class Solution:
def minWindow(self, s, t):
table = {}
for c in t:
table[c] = table[c] + 1 if c in table else 1
start, end, count = 0, 0, len(t)
min_start, min_end = None, None
for i in range(len(s)):
if s[i] in table:
if table[s[i]] > 0:
count -= 1
table[s[i]] -= 1
if count == 0:
while count == 0:
if s[start] in table:
if table[s[start]] >= 0:
count += 1
if min_start == None or i - start < min_end - min_start:
min_start, min_end = start, i
table[s[start]] += 1
start += 1
if min_start == None:
return ""
else:
return s[min_start : min_end + 1]
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NONE NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NONE BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NONE RETURN STRING RETURN VAR VAR BIN_OP VAR NUMBER
|
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
|
class Solution:
def minWindow(self, s, t):
left = -1
right = 0
result = ""
totalMatch = 0
d = {}
for c in t:
d[c] = d.get(c, 0) + 1
for right in range(len(s)):
c = s[right]
d[c] = d.get(c, 0) - 1
if d[c] >= 0:
totalMatch += 1
if totalMatch == len(t):
totalMatch -= 1
left += 1
while d[s[left]] < 0:
d[s[left]] += 1
left += 1
d[s[left]] += 1
if result == "" or len(result) > right - left:
result = s[left : right + 1]
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR STRING FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
|
class Solution:
def minWindow(self, s, t):
if len(t) > len(s):
return ""
dic = {}
for i in t:
if i in dic:
dic[i] += 1
else:
dic[i] = 1
counter = len(dic)
start = end = 0
minl = len(s) + 1
res = ""
while end < len(s):
j = s[end]
if j in dic:
dic[j] -= 1
if dic[j] == 0:
counter -= 1
end += 1
while counter == 0:
i = s[start]
if i in dic:
dic[i] += 1
if dic[i] > 0:
counter += 1
if end - start < minl:
minl = end - start
res = s[start:end]
start += 1
return res
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR
|
Given an integer array arr of size N and an integer k, return the number of good subarrays of arr.
A good array is an array where the number of different integers in that is exactly k.
For example, {1, 3, 4, 4, 1} has 3 different integers: 1, 3, and 4.
Note : A subarray is a contiguous part of an array.
Example 1:
Input:
N = 5
k = 2
arr[ ] = {1, 2, 1, 2, 3}
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: {1, 2}, {2, 1}, {1, 2}, {2, 3}, {1, 2, 1}, {2, 1, 2}, {1, 2, 1, 2}.
Example 2:
Input:
N = 5
k = 3
arr[ ] = {1, 2, 1, 3, 4}
Output: 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function subarrayCount() which takes the array of integers arr , an integer N and k as parameters and returns a number of good subarrays.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ arr_{i }≤ N
|
class Solution:
def subarrayCount(self, arr, N, k):
def atmost(arr, k):
l, r, count = 0, 0, 0
mp = {}
while r < len(arr):
mp[arr[r]] = 1 + mp.get(arr[r], 0)
while len(mp) > k:
mp[arr[l]] -= 1
if mp[arr[l]] == 0:
mp.pop(arr[l])
l += 1
count += r - l + 1
r += 1
return count
return atmost(arr, k) - atmost(arr, k - 1)
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
Given an integer array arr of size N and an integer k, return the number of good subarrays of arr.
A good array is an array where the number of different integers in that is exactly k.
For example, {1, 3, 4, 4, 1} has 3 different integers: 1, 3, and 4.
Note : A subarray is a contiguous part of an array.
Example 1:
Input:
N = 5
k = 2
arr[ ] = {1, 2, 1, 2, 3}
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: {1, 2}, {2, 1}, {1, 2}, {2, 3}, {1, 2, 1}, {2, 1, 2}, {1, 2, 1, 2}.
Example 2:
Input:
N = 5
k = 3
arr[ ] = {1, 2, 1, 3, 4}
Output: 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function subarrayCount() which takes the array of integers arr , an integer N and k as parameters and returns a number of good subarrays.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ arr_{i }≤ N
|
class Solution:
def atmost(self, arr, k):
left = 0
suma = 0
d = dict()
for i in range(len(arr)):
if arr[i] in d:
d[arr[i]] += 1
else:
d[arr[i]] = 1
while len(d) > k:
d[arr[left]] -= 1
if d[arr[left]] == 0:
del d[arr[left]]
left += 1
suma += i - left + 1
return suma
def subarrayCount(self, arr, N, k):
return self.atmost(arr, k) - self.atmost(arr, k - 1)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
|
Given an integer array arr of size N and an integer k, return the number of good subarrays of arr.
A good array is an array where the number of different integers in that is exactly k.
For example, {1, 3, 4, 4, 1} has 3 different integers: 1, 3, and 4.
Note : A subarray is a contiguous part of an array.
Example 1:
Input:
N = 5
k = 2
arr[ ] = {1, 2, 1, 2, 3}
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: {1, 2}, {2, 1}, {1, 2}, {2, 3}, {1, 2, 1}, {2, 1, 2}, {1, 2, 1, 2}.
Example 2:
Input:
N = 5
k = 3
arr[ ] = {1, 2, 1, 3, 4}
Output: 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function subarrayCount() which takes the array of integers arr , an integer N and k as parameters and returns a number of good subarrays.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ arr_{i }≤ N
|
class Solution:
def subarrayCount(self, arr, N, k):
return self.subArrayMax(arr, N, k) - self.subArrayMax(arr, N, k - 1)
def subArrayMax(self, arr, N, k):
i, j, ans = 0, 0, 0
state = {}
for j in range(N):
state[arr[j]] = 1 + state.get(arr[j], 0)
while len(state) > k:
state[arr[i]] -= 1
if state[arr[i]] == 0:
state.pop(arr[i])
i += 1
ans += j - i + 1
return ans
|
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given an integer array arr of size N and an integer k, return the number of good subarrays of arr.
A good array is an array where the number of different integers in that is exactly k.
For example, {1, 3, 4, 4, 1} has 3 different integers: 1, 3, and 4.
Note : A subarray is a contiguous part of an array.
Example 1:
Input:
N = 5
k = 2
arr[ ] = {1, 2, 1, 2, 3}
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: {1, 2}, {2, 1}, {1, 2}, {2, 3}, {1, 2, 1}, {2, 1, 2}, {1, 2, 1, 2}.
Example 2:
Input:
N = 5
k = 3
arr[ ] = {1, 2, 1, 3, 4}
Output: 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function subarrayCount() which takes the array of integers arr , an integer N and k as parameters and returns a number of good subarrays.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ arr_{i }≤ N
|
class Solution:
def subarrayCount(self, arr, N, k):
return self.atMost(arr, N, k) - self.atMost(arr, N, k - 1)
def atMost(self, arr, n, k):
i = 0
j = 0
count = 0
map = {}
while j < n:
map[arr[j]] = map.get(arr[j], 0) + 1
while len(map) > k:
map[arr[i]] -= 1
if map[arr[i]] == 0:
del map[arr[i]]
i += 1
count += j - i + 1
j += 1
return count
|
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an integer array arr of size N and an integer k, return the number of good subarrays of arr.
A good array is an array where the number of different integers in that is exactly k.
For example, {1, 3, 4, 4, 1} has 3 different integers: 1, 3, and 4.
Note : A subarray is a contiguous part of an array.
Example 1:
Input:
N = 5
k = 2
arr[ ] = {1, 2, 1, 2, 3}
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: {1, 2}, {2, 1}, {1, 2}, {2, 3}, {1, 2, 1}, {2, 1, 2}, {1, 2, 1, 2}.
Example 2:
Input:
N = 5
k = 3
arr[ ] = {1, 2, 1, 3, 4}
Output: 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function subarrayCount() which takes the array of integers arr , an integer N and k as parameters and returns a number of good subarrays.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ arr_{i }≤ N
|
class Solution:
def solve(self, arr, N, k):
count, i = 0, 0
d = {}
for j in range(N):
d[arr[j]] = 1 + d.get(arr[j], 0)
while i <= j and len(d) > k:
d[arr[i]] = -1 + d.get(arr[i], 0)
if not d[arr[i]]:
del d[arr[i]]
i += 1
count += j - i + 1
return count
def subarrayCount(self, arr, N, k):
return self.solve(arr, N, k) - self.solve(arr, N, k - 1)
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
|
Given an integer array arr of size N and an integer k, return the number of good subarrays of arr.
A good array is an array where the number of different integers in that is exactly k.
For example, {1, 3, 4, 4, 1} has 3 different integers: 1, 3, and 4.
Note : A subarray is a contiguous part of an array.
Example 1:
Input:
N = 5
k = 2
arr[ ] = {1, 2, 1, 2, 3}
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: {1, 2}, {2, 1}, {1, 2}, {2, 3}, {1, 2, 1}, {2, 1, 2}, {1, 2, 1, 2}.
Example 2:
Input:
N = 5
k = 3
arr[ ] = {1, 2, 1, 3, 4}
Output: 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function subarrayCount() which takes the array of integers arr , an integer N and k as parameters and returns a number of good subarrays.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ arr_{i }≤ N
|
class Solution:
def subarrayCount(self, nums, N, k):
ans = 0
n = len(nums)
latest = {}
unique = 0
prev = 0
for i in range(n):
if nums[i] not in latest:
unique += 1
latest[nums[i]] = i
if unique > k:
while True:
if latest[nums[prev]] == prev:
latest.pop(nums[prev])
prev += 1
unique -= 1
break
else:
prev += 1
if unique == k:
ans += 1
tmp = prev
while True:
if latest[nums[tmp]] != tmp:
ans += 1
tmp += 1
else:
break
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR WHILE NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.