Skip to content

techtrendings.com

Let's explore

Menu
Menu

Maximum of all Subarrays of size K | LeetCode #239

Posted on August 5, 2022August 5, 2022 by Avidlearner

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the max sliding window of size K

Example 1:

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Explanation: 
Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7


One can solve this problem by Brute Force method by running two loops and find the maximum in each sliding window of size k, but that will be O(n*n). We need to optimize it further for that we can use Deque which will be Monotonic(Decreasing) in nature such that it always has front element as maximum element and also we will only store the index in the deque so that we can access the element directly to check. Below is the code implementation

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        deque<int> dq;
        vector<int> res;
        for (int i=0; i<nums.size(); i++) 
        {

            //Check if the maximum element is at first position in the sliding window, pop it from deque to get next maximum in next sliding window
            if (!dq.empty() && dq.front() == i-k) 
               dq.pop_front();


           //This loop is to ensure the deque is in decreasing order
            while (!dq.empty() && nums[dq.back()] < nums[i])
                dq.pop_back();
            dq.push_back(i);

            if (i>=k-1) 
               res.push_back(nums[dq.front()]);
        }
        return res;
    }
};

I hope you understands the algorithm and solution. If you still has doubt .Pleas leave a comment.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Implement Trie Data Structure in C++- LeetCode
  • How TLS Works
  • C++ – Factory Design Pattern – Creation Design Pattern
  • C++ – Strategy Design Pattern – Behavioral Design Pattern
  • LFU Cache Implementation – LeetCode

Recent Comments

  • automatically like friends photos on instagram on Program to find unpaired element in an Array in C++|Leetcode |techtrendings
  • Twicsy on Program to find unpaired element in an Array in C++|Leetcode |techtrendings

Archives

  • January 2023
  • November 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Algorithm
  • Algorithm
  • C++
  • Design Patterns
  • Multithreading
  • OS Concepts
  • Programming
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Join Our Mailing List for the Latest News and Updates.

© 2023 techtrendings.com | Powered by Superbs Personal Blog theme