Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sliding window max

vector<int> solve(vector<int>& nums, int k) {
    vector<int>ans;
    if(k>nums.size())return ans;
    deque<int>dq;
    for(int i = 0; i < k; i++)
    {
        while(!dq.empty() && 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++)
    {
        if(!dq.empty() && dq.front()<=i-k)dq.pop_front();
        while(!dq.empty() && nums[dq.back()]<=nums[i])
        {
            dq.pop_back();
        }
        dq.push_back(i);
        ans.push_back(nums[dq.front()]);
    }
    return ans;
}
Comment

sliding window maximum

# Python program to find the maximum for
# each and every contiguous subarray of
# size k
 
from collections import deque
 
# A Deque (Double ended queue) based
# method for printing maximum element
# of all subarrays of size k
def printMax(arr, n, 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"""
    Qi = deque()
     
    # Process first k (or first window)
    # elements of array
    for i in range(k):
       
        # For every element, the previous
        # smaller elements are useless
        # so remove them from Qi
        while Qi and arr[i] >= arr[Qi[-1]] :
            Qi.pop()
         
        # Add new element at rear of queue
        Qi.append(i);
         
    # Process rest of the elements, i.e.
    # from arr[k] to arr[n-1]
    for i in range(k, n):
         
        # The element at the front of the
        # queue is the largest element of
        # previous window, so print it
        print(str(arr[Qi[0]]) + " ", end = "")
         
        # Remove the elements which are
        # out of this window
        while Qi and Qi[0] <= i-k:
             
            # remove from front of deque
            Qi.popleft()
         
        # Remove all elements smaller than
        # the currently being added element
        # (Remove useless elements)
        while Qi and arr[i] >= arr[Qi[-1]] :
            Qi.pop()
         
        # Add current element at the rear of Qi
        Qi.append(i)
     
    # Print the maximum element of last window
    print(str(arr[Qi[0]]))
     
# Driver code
if __name__=="__main__":
    arr = [12, 1, 78, 90, 57, 89, 56]
    k = 3
    printMax(arr, len(arr), k)
     
# This code is contributed by Shiv Shankar
Comment

PREVIOUS NEXT
Code Example
Python :: Doubleclick .py Prep Mac 
Python :: django qurry 
Python :: get weather data from weather underground 
Python :: how call a class in another class python 
Python :: square root in python numpy 
Python :: menjumlahkan elemen tertentu pada list dalam dictionary python 
Python :: Data type based on rows 
Python :: unittest run one test 
Python :: Palindrome in Python Using reverse function 
Python :: walk nested dict python 
Python :: Find Factors of a Number Using Class 
Python :: Using Python Permutations function on a String with extra parameter 
Python :: how to get class names in predict_proba 
Python :: lime python interpretation 
Python :: for loop for many integers in list 
Python :: python how to do imports 
Python :: how to add item to a list in pithon 
Python :: cartopy indicate lat lon 
Python :: Javascript rendering problem in requests-html 
Python :: Python NumPy ndarray flatten Function Example 02 
Python :: django.db.utils.operationalerror: (1051, "unknown table 
Python :: Python NumPy asscalar Function Syntax 
Python :: button to redirect to another tree view in odoo 
Python :: max index tuple 
Python :: pyqt log widget thread safe 
Python :: pandas use 3 columns for 2d distribution 
Python :: django filter empty onetoone exists 
Python :: dictionary display 
Python :: print python age input 
Python :: city of stars how many words in a song python code 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =