category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Sliding Window Maximum

multiset
class Solution {
    public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        int l = 0, r = 0;
        multiset<int> st;
        vector<int> ans;
        for (r = 0; r < k; r++)
            st.insert(nums[r]);

        ans.push_back(*st.rbegin());

        // maintaining a sliding window of [l .... .r]
        for (r = k; r < nums.size(); r++) {
            st.insert(nums[r]);
            st.erase(st.find(nums[l]));
            l ++;
            ans.push_back(*st.rbegin());
        }
        return ans;
    }
};
monotonic deque
class Solution {
    public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        deque<int> dq;
        vector<int> ans;

        for (int i = 0; i < k; i++) {
            while (!dq.empty() and nums[dq.back()] < nums[i])
                dq.pop_back();
            dq.push_back(i);
        }

        ans.push_back(nums[dq.front()]);

        for (int i = k; i < nums.size(); i++) {
            while (!dq.empty() and dq.front() <= i - k      )  dq.pop_front();
            while (!dq.empty() and nums[dq.back()] < nums[i])  dq.pop_back();
            dq.push_back(i);
            ans.push_back(nums[dq.front()]);
        }

        return ans;
    }
};

public int[] maxSlidingWindow(int[] a, int k) {
		if (a == null || k <= 0) {
			return new int[0];
		}
		int n = a.length;
		int[] r = new int[n-k+1];
		int ri = 0;

		Deque<Integer> q = new ArrayDeque<>();
		for (int i = 0; i < a.length; i++) {

			while (!q.isEmpty() && q.peekFirst() < i - k + 1) {
				q.pollFirst(); // or poll(), since queue so by default first one is popped out.
			}

			while (!q.isEmpty() && a[q.peekLast()] < a[i]) {
				q.pollLast();
			}

			q.offer(i);
			if (i >= k - 1) {
				r[ri++] = a[q.peekFirst()];
			}
		}
		return r;
	}