I was solving the Sliding Window Maximum problem but I'm receiving the following error on Leetcode but it's working on my local compiler [VSCode]
Line 1034: Char 34: runtime error: addition of unsigned offset to 0x603000000040 overflowed to 0x603000000034 (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34
Here's the code:
#include <deque>
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& A, int k) {
int _max = INT_MIN;
vector<int> ans;
for (int i = 0; i < k; ++i)
{
_max = max(_max, A[i]);
}
if (k == A.size()){
return {_max};
}
ans.push_back(_max);
deque<int> dq;
dq.push_back(_max);
for (int i = k; i < A.size(); ++i)
{
if (dq.empty())
{
dq.push_back(i);
_max = A[i];
}
else
{
while (!dq.empty() && A[dq.front()] < A[i])
{
dq.pop_front();
}
if (dq.empty())
{
_max = A[i];
}
else
{
_max = max(_max, A[i]);
}
dq.push_front(A[i]);
ans.push_back(_max);
}
}
return ans;
}
};