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 !! :)
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 !! :)
Coding
ReplyDeleteHoliday 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/
Nice Content. Really you have done a good job. Thanks for sharing this valuable information....
ReplyDeleteMobile Automation Using Appium
Mobile App Automation Testing
Excellent data with lots of information. I have bookmarked this page for my future reference. Do share more updates.
ReplyDeleteSalesforce Mobile App Development
Salesforce App Development
sterwork. youve done a excellent activity on this subject!
ReplyDeleteIts 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
ncomplicated, appealing and simple and easy for customers..hold up the great function !
ReplyDeleteGood post, I just IMd the link to a buddy who was doing a little research on this. Thanks again.
tSarah Berger
Wow this is amazing post you hvae shared with us. It gave me a new puspactive. Tha k you so much for sharing.
ReplyDeletebest electric kettles under 500
best electric kettles under 1000
best microwave oven in India
best mixer grinder in India
What a fantastic resource that you point out here, happy to see such informative stuff.
ReplyDeleteThanks 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!
I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.
ReplyDeletecreate taxi app
Such a well-researched article! It’s clear you put a lot of effort into this. I’ll be bookmarking this for future reference. Thanks for sharing!
ReplyDeleteAndaman and Nicobar Package for Family
Best Time to Visit the Andaman
Flora and Fauna of Andaman and Nicobar Islands