7 Feb 2020

Sliding Window Maxima

Problem Statement : 
        Given an array of size n and window of size k (k < n) , find the maximum element in each contagious array of size k(also can be called as window) .

Leetcode:
239. Sliding Window Maximum
https://leetcode.com/problems/sliding-window-maximum/


Solution : 

There are multiple approaches to solve this problem, we will be solving the problem with the help of deque(double ended queue). The brute force approach would take O(n * k) time complexity as for each n , you will have to traverse next k elements.

Below is the implementation with the help of Deque, important thing to note here is that
i. all elements which are getting out of the window range needs to be removed and
ii. if there is a new larger incoming element coming in as part of new item, remove all smaller elements in the deque.


Below is the code implementation :


Please post comments and suggestions.
Happy Coding !! :)

8 comments:

  1. Coding

    Holiday Camps - Get the best coding and STEM education in Singapore. Bricks 4 Kidz offer the best STEM and Coding Enrichment platform for your child. Join us now!

    to get more - https://www.bricks4kidz.com.sg/about-us/

    ReplyDelete
  2. Nice Content. Really you have done a good job. Thanks for sharing this valuable information....
    Mobile Automation Using Appium
    Mobile App Automation Testing

    ReplyDelete
  3. Excellent data with lots of information. I have bookmarked this page for my future reference. Do share more updates.
    Salesforce Mobile App Development
    Salesforce App Development

    ReplyDelete
  4. sterwork. youve done a excellent activity on this subject!
    Its such as you read my thoughts! You appear to know so much approximately this, such as you wrote the e book in it or something. I feel that you just could do with a feSarah Berger

    ReplyDelete
  5. ncomplicated, appealing and simple and easy for customers..hold up the great function !
    Good post, I just IMd the link to a buddy who was doing a little research on this. Thanks again.
    tSarah Berger

    ReplyDelete
  6. Wow this is amazing post you hvae shared with us. It gave me a new puspactive. Tha k you so much for sharing.
    best electric kettles under 500
    best electric kettles under 1000
    best microwave oven in India
    best mixer grinder in India

    ReplyDelete
  7. What a fantastic resource that you point out here, happy to see such informative stuff.
    Thanks a lot for sharing. I appreciate your hard efforts.
    Good luck!

    Best 3D Printers:

    B9 Printer

    B9 3D Printer

    B9 Core 550

    B9 Creator v1.2

    Cheers!

    ReplyDelete
  8. I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.
    create taxi app

    ReplyDelete