Skip to content

Commit 266d6ee

Browse files
authored
Update 1438.Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit_heap.cpp
1 parent d3838bf commit 266d6ee

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

Deque/1438.Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/1438.Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit_heap.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,25 @@ class Solution {
22
public:
33
int longestSubarray(vector<int>& nums, int limit)
44
{
5-
multiset<int>Set;
5+
multiset<int>Set({nums[0]});
66
int j = 0;
77
int ret = 0;
88

99
for (int i=0; i<nums.size(); i++)
1010
{
11-
while (j<nums.size() && (Set.size()==0 || *Set.rbegin()-*Set.begin()<=limit))
11+
while (*Set.rbegin()-*Set.begin()<=limit)
1212
{
13-
Set.insert(nums[j]);
13+
ret = max(ret, j-i+1);
1414
j++;
15+
if (j>=nums.size()) break;
16+
Set.insert(nums[j]);
1517
}
16-
17-
if (*Set.rbegin()-*Set.begin()>limit)
18-
{
19-
j--;
20-
Set.erase(Set.find(nums[j]));
21-
}
22-
23-
ret = max(ret, (int)Set.size());
24-
25-
if (i>=0) Set.erase(Set.find(nums[i]));
2618

19+
if (j>=nums.size()) break;
20+
21+
Set.erase(Set.find(nums[i]));
2722
}
2823

2924
return ret;
30-
3125
}
3226
};

0 commit comments

Comments
 (0)