Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find max values of sublists of size k in Python
Finding maximum values in sliding windows of size k is a common problem. We'll explore three approaches: brute force, optimized tracking, and using a deque for efficient sliding window maximum.
Problem Understanding
Given a list nums = [12, 7, 3, 9, 10, 9] and k = 3, we need to find the maximum value in each sublist of size 3:
[12, 7, 3] ? max = 12
[7, 3, 9] ? max = 9
[3, 9, 10] ? max = 10
[9, 10, 9] ? max = 10
Method 1: Brute Force Approach
The simplest approach is to check each window and find its maximum value ?
def find_max_sublists_brute(nums, k):
if k > len(nums):
return []
result = []
for i in range(len(nums) - k + 1):
window = nums[i:i + k]
result.append(max(window))
return result
# Test the function
nums = [12, 7, 3, 9, 10, 9]
k = 3
print(find_max_sublists_brute(nums, k))
[12, 9, 10, 10]
Method 2: Optimized Tracking
This approach tracks the maximum element and its position to avoid recalculating when possible ?
def find_max_sublists_optimized(nums, k):
if k > len(nums):
return []
result = []
max_val = nums[0]
max_pos = 0
# Find max in first window
for i in range(k):
if nums[i] > max_val:
max_val = nums[i]
max_pos = i
result.append(max_val)
# Process remaining windows
for i in range(k, len(nums)):
# If current element is greater than or equal to max_val
if nums[i] >= max_val:
max_val = nums[i]
max_pos = i
# If max element is outside current window
elif i - max_pos >= k:
max_pos = i - k + 1
max_val = nums[max_pos]
for j in range(i - k + 1, i + 1):
if nums[j] > max_val:
max_val = nums[j]
max_pos = j
result.append(max_val)
return result
# Test the function
nums = [12, 7, 3, 9, 10, 9]
k = 3
print(find_max_sublists_optimized(nums, k))
[12, 9, 10, 10]
Method 3: Using Deque (Most Efficient)
The most efficient approach uses a deque to maintain potential maximum candidates ?
from collections import deque
def find_max_sublists_deque(nums, k):
if k > len(nums):
return []
result = []
dq = deque() # Store indices
for i in range(len(nums)):
# Remove indices outside current window
while dq and dq[0] <= i - k:
dq.popleft()
# Remove smaller elements from back
while dq and nums[dq[-1]] <= nums[i]:
dq.pop()
dq.append(i)
# Add to result if window is complete
if i >= k - 1:
result.append(nums[dq[0]])
return result
# Test the function
nums = [12, 7, 3, 9, 10, 9]
k = 3
print(find_max_sublists_deque(nums, k))
[12, 9, 10, 10]
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Brute Force | O(n×k) | O(1) | Small arrays or k values |
| Optimized Tracking | O(n×k) worst case | O(1) | Arrays with few maximum changes |
| Deque | O(n) | O(k) | Large arrays, optimal solution |
Conclusion
Use the deque approach for optimal O(n) time complexity. The brute force method is simpler for small datasets, while optimized tracking works well when maximum values don't change frequently across windows.
