Deque-STL

Sort by

recency

|

446 Discussions

|

  • + 0 comments

    You need to find the max of the K values. Then while you do not will pop the max value, and not arrive the end of queue, continue comparing to the next value, printing the max and pop_front of queue. If the value to pop is the max value, then start again and find the max of the K values.

    This is not optimized and if a lot of max values are repeated the complexity will increment. But this code pass all the tests in time.

    void printKMax(int arr[], int n, int k){
    	//Write your code here.
        deque<int> fila;
        deque<int>::iterator itr;
        int max = 0;
        
        for (int i = 0; i<n; i++)
        {
            fila.push_back(arr[i]);
        }
        
        while(fila.size() >= k)
        {
            itr = fila.begin();
            for(int i = 0; i<k ; i++)
            {
                if(*itr > max)
                    max = *itr;
                itr++;
            }
            cout << max << " ";
            while ((fila.front() != max) && (itr != fila.end()))
            {
                if (max < *itr)
                    max = *itr;
                cout << max << " ";
                itr++;
                fila.pop_front();
            }
            fila.pop_front();
            max = 0;
        }
        cout << endl;
    }
    
  • + 0 comments

    include

    include

    using namespace std;

    void printKMax(int arr[], int n, int k){ // //Write your code here. // int i = 0; // int j = k-1; // while(jmaxi) maxi = arr[k]; // } // cout< dq;

    // Process the first 'k' elements.
    for (int i = 0; i < k; i++) {
        // Remove elements smaller than the current one.
        while (!dq.empty() && arr[i] >= arr[dq.back()]) {
            dq.pop_back();
        }
        dq.push_back(i);
    }
    
    // Process the remaining elements.
    for (int i = k; i < n; i++) {
        // The front of the deque is the largest element of the previous window.
        cout << arr[dq.front()] << " ";
    
        // Remove elements that are out of this window.
        while (!dq.empty() && dq.front() <= i - k) {
            dq.pop_front();
        }
    
        // Remove elements smaller than the current one.
        while (!dq.empty() && arr[i] >= arr[dq.back()]) {
            dq.pop_back();
        }
    
        dq.push_back(i);
    }
    
    // Print the maximum of the last window.
    cout << arr[dq.front()] << endl;
    

    }

    int main(){

    int t;
    cin >> t;
    while(t>0) {
        int n,k;
        cin >> n >> k;
        int i;
        int arr[n];
        for(i=0;i<n;i++)
            cin >> arr[i];
        printKMax(arr, n, k);
        t--;
    }
    return 0;
    

    }

  • + 2 comments

    I had a solution which pushed and pops, but that exceeded time limits. But the straight-forward solution below did as well. Anybody any clues?

    void printKMax(int arr[], int n, int k){
    	//Write your code here.
    	std::deque <int>que;
    	int cnt;
    	for (cnt=0 ; cnt<n ; cnt++) que.push_back(arr[cnt]); // just push them all
    	for (cnt=0;cnt<n-k;cnt++)
    		std::cout << *std::max_element(que.begin()+cnt,que.begin()+cnt+k) << ' ';
    	std::cout << *std::max_element(que.begin()+cnt,que.begin()+cnt+k) << std::endl;
    }
    
  • + 0 comments

    //This is the simplest implementation of all and passes all test cases

    include

    include

    using namespace std;

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t; cin>>t; while(t--) { int n,k; cin>>n>>k; int arr[n]; for(int i=0;i>arr[i]; } deque deq;//contains the indices of max elements of arr for(int i=0;i

            /*this checks if the i'th element is greater than the back of 
            deq which is the previously checked and added indice*/
            while(!deq.empty()&& arr[i]>arr[deq.back()])
                deq.pop_back();
            deq.push_back(i);
            if(i>=k-1)//this prints past the first window
                cout<<arr[deq.front()]<<" ";
        }
        cout<<endl;      
    }
    return 0;
    

    }

  • + 0 comments

    include

    include

    include

    include

    using namespace std;

    void printMaxInSubarrays(const deque& dq, int k) {

    deque<int> indices; // To store indices of potential maximums
    
    for (int i = 0; i < dq.size(); ++i) {
        // Remove indices that are out of the current window
        while (!indices.empty() && indices.front() <= i - k) {
            indices.pop_front();
        }
    
        // Remove elements that are less than the current element
        while (!indices.empty() && dq[indices.back()] <= dq[i]) {
            indices.pop_back();
        }
    
        // Add current element's index at the end of the deque
        indices.push_back(i);
    
        // The first k-1 windows will not be complete, so skip printing max
        if (i >= k - 1) {
            cout << dq[indices.front()] << " ";
        }
    }
    cout << endl;
    

    }

    int main(){

    int t,s,k;
    cin>>t;
    
    vector<deque<int> > all_deques;
    
    
    
    for(int i=0;i<t;i++){
        cin>>s>>k;
    
        deque<int> my_deque;
    
        for(int i=0;i<s;i++){
            int value;
            cin>>value;
            my_deque.push_back(value);
        }
    
        all_deques.push_back(my_deque);
    
    }
    
    
    
        for(const auto& my_deque : all_deques){
            printMaxInSubarrays(my_deque,k);
        }
    
    
    
    
    return 0;
    

    }