mardi 3 septembre 2019

Why do I get timeouts in this hackerrank deque problem?

I'm solving hackerrank's deque problem(Sliding window using deque, link: https://www.hackerrank.com/challenges/deque-stl/problem). I ran into a timeout problem although my complexity should be of O(n). I'm also not sure if my logic is 100% right since only 1st case passes.

I've tried changing C++ to C++14, changed all couts and cins to scanf and printf.

This is my code.

void printKMax(int arr[], int n, int k)  
{  
    int max, temp;
    deque<int>queue;
    temp = k-1;

    for (int i = 0; i < n; i++)
    {
        queue.push_back(arr[i]);

    }

    max = queue[temp];
    while(queue.size()>=k)
    {
        if(temp==0)
        {
            if (queue[temp] > max)
                max = queue[temp];
            cout << max << " ";
            temp=k-1;
            max = queue[temp];
            queue.pop_front();
        }
        if (queue[temp] > max)
            max = queue[temp];
        temp--;

    }

    cout << endl;


}

Main:

#include <iostream>
#include <deque> 
#include <iostream> 

using namespace std; 

int main(){

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

Working code that I can't understand quite well from: https://www.geeksforgeeks.org/sliding-window-maximum-maximum-of-all-subarrays-of-size-k/#disqus_thread

void printKMax(int arr[], int n, int k) 
{ 
    // Create a Double Ended Queue, Qi that will store indexes of array elements 
    // The queue will store indexes of useful elements in every window and it will 
    // maintain decreasing order of values from front to rear in Qi, i.e., 
    // arr[Qi.front[]] to arr[Qi.rear()] are sorted in decreasing order 
    std::deque<int> Qi(k); 

    /* Process first k (or first window) elements of array */
    int i; 
    for (i = 0; i < k; ++i) { 
        // For every element, the previous smaller elements are useless so 
        // remove them from Qi 
        while ((!Qi.empty()) && arr[i] >= arr[Qi.back()]) 
            Qi.pop_back(); // Remove from rear 

        // Add new element at rear of queue 
        Qi.push_back(i); 
    } 

    // Process rest of the elements, i.e., from arr[k] to arr[n-1] 
    for (; i < n; ++i) { 
        // The element at the front of the queue is the largest element of 
        // previous window, so print it 
        cout << arr[Qi.front()] << " "; 

        // Remove the elements which are out of this window 
        while ((!Qi.empty()) && Qi.front() <= i - k) 
            Qi.pop_front(); // Remove from front of queue 

        // Remove all elements smaller than the currently 
        // being added element (remove useless elements) 
        while ((!Qi.empty()) && arr[i] >= arr[Qi.back()]) 
            Qi.pop_back(); 

        // Add current element at the rear of Qi 
        Qi.push_back(i); 
    } 

    // Print the maximum element of last window 
    cout << arr[Qi.front()]; 
} 

Aucun commentaire:

Enregistrer un commentaire