Python – K consecutive Maximum
Given a List, find maximum of next K elements from each index.
Input : test_list = [4, 3, 9, 2, 6, 12, 4, 3, 2, 4, 5], K = 4
Output : [9, 9, 9, 12, 12, 12, 4, 5]
Explanation : Max of next 4 elements, (max(4, 3, 9, 2) = 9)
Input : test_list = [4, 3, 9, 2, 6], K = 4
Output : [9, 9]
Explanation : Max of next 4 elements, (max(4, 3, 9, 2) = 9)
Method #1 : Using loop + max() + slicing
In this, we iterate for elements in loop, slicing till next K, and use max() to get maximum of them for current index.
Python3
test_list = [ 4 , 3 , 8 , 2 , 6 , 7 , 4 , 3 , 2 , 4 , 5 ]
print ( "The original list is : " + str (test_list))
K = 4
res = []
for idx in range ( len (test_list) - K + 1 ):
res.append( max (test_list[idx : idx + K]))
print ( "Next K Maximum List : " + str (res))
|
Output
The original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using list comprehension
This is yet another way to solve this, one-liner alternative to above method using list comprehension.
Python3
test_list = [ 4 , 3 , 8 , 2 , 6 , 7 , 4 , 3 , 2 , 4 , 5 ]
print ( "The original list is : " + str (test_list))
K = 4
res = [ max (test_list[idx : idx + K]) for idx in range ( len (test_list) - K + 1 )]
print ( "Next K Maximum List : " + str (res))
|
Output
The original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]
Method 3: Using a deque
- Initialize an empty deque to store the indices of elements in the current window.
- Initialize a list res to store the K consecutive maximum values.
- Loop through the input list test_list, starting at index 0.
- In each iteration, add the current index to the right end of the deque.
- If the length of the deque is greater than K, remove the leftmost index (i.e. the index from the previous window).
- If the index is greater than or equal to K – 1:
a. Append the maximum value in the current window (i.e. the value at the left end of the deque) to the res list.
b. If the maximum value in the current window is equal to the value at the left end of the deque, pop the leftmost index from the deque.
- Return the res list.
Python3
from collections import deque
test_list = [ 4 , 3 , 8 , 2 , 6 , 7 , 4 , 3 , 2 , 4 , 5 ]
print ( "The original list is : " + str (test_list))
K = 4
dq = deque()
res = []
for i in range ( len (test_list)):
dq.append(i)
if len (dq) > K:
dq.popleft()
if i > = K - 1 :
window = [test_list[j] for j in dq]
res.append( max (window))
if dq[ 0 ] = = i - K + 1 :
dq.popleft()
print ( "Next K Maximum List : " + str (res))
|
Output
The original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k), where k is the window size.
Last Updated :
01 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...