
class Solution {
public:
int longestSubarray(vector<int>& nums, int limit) {
multiset<int> s;
int n = nums.size();
int left = 0, right = 0;
int maxlen = 0;
while (right < n)
{
s.insert(nums[right]);
while (*s.rbegin() - *s.begin() > limit)
{
s.erase(s.find(nums[left]));
left++;
}
maxlen = max(maxlen, right - left + 1);
right++;
}
return maxlen;
}
};
这段代码实现了一个寻找数组中最大子数组长度的算法,其中子数组的最大差值不超过给定限制。它使用了多集(multiset)数据结构,并通过滑动窗口的方法动态维护满足条件的子数组。
786

被折叠的 条评论
为什么被折叠?



