Deque-STL

  • + 1 comment

    You are always comparing the full deque. You could first test if the new number is the biggest one in the deque now, or if the biggest isn't the one you're popping now nor the new come in... anyway, I can't explain too good, but you can see my code:

    int max(deque<int> myDeque){
        
        int maximo = myDeque[0];
        for (int i=0; i<myDeque.size(); i++){
            if(maximo<myDeque[i]){
                maximo = myDeque[i];
            }
        }
        return maximo;
    }
    
    void printKMax(int arr[], int n, int k){
        deque<int> myDeque;
        int maxActual;
        for (int i=0; i < k; i++){
            myDeque.push_back(arr[i]);        
        }
        maxActual=max(myDeque);
        cout << maxActual<< " ";
        for(int i=0; i<n-k; i++){
            if(maxAtual!= myDeque.front() && maxAtual > arr[i+k]){
                cout << maxActual << " ";
                myDeque.pop_front();
                myDeque.push_back(arr[i+k]);
            }
            else if(maxAtual!= myDeque.front() && maxAtual < arr[i+k]){
                maxActual = arr[i+k];
                cout << maxActual << " ";
                myDeque.pop_front();
                myDeque.push_back(arr[i+k]);
            }
            else{
                myDeque.pop_front();
                myDeque.push_back(arr[i+k]);
                maxActual =  max(myDeque);
                cout << maxActual << " ";
            }
        }
        cout << endl;
    }