Couldn't pass tests, any help? G4G solution doesn't work either


#1
int Solution::solve(vector<int> &a, int k) {
    long long res = 0;    
    deque<int> s, g;
    for (int i = 0; i < a.size(); i++) {
        while (!s.empty() && a[i] >= a[s.front()]) s.pop_front();
        while (!g.empty() && a[i] <= a[g.front()]) g.pop_front();
        s.push_front(i);
        g.push_front(i);
        if (s.back() <= i - k) s.pop_back();
        if (g.back() <= i - k) g.pop_back();
        if (i >= k - 1) res = (res + a[s.back()] + a[g.back()]) % 1000000007;
    }
    return res;
}

#2

Got it, it needs a special modulo function (all positive).


#3

Bro, your comment helped me save a lot of time. Thank you