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 length of longest sublist with given condition in Python
Suppose we have a list of numbers called nums. We need to find the length of the longest sublist where 2 * minimum of sublist > maximum of sublist.
So, if the input is like nums = [10, 2, 6, 6, 4, 4], then the output will be 4, as the sublist [6, 6, 4, 4] is the longest sublist that satisfies the condition since 2 * 4 > 6.
Algorithm
We use a sliding window approach with two deques to efficiently track minimum and maximum values in the current window ?
Use two deques:
minqfor tracking minimum andmaxqfor tracking maximumMaintain a sliding window with left pointer
land right pointerrExpand the window by moving
rand shrink it by movinglwhen condition failsKeep track of the maximum valid window size encountered
Example
from collections import deque
def find_longest_sublist(nums):
ret = 0
minq, maxq = deque(), deque()
l, r = 0, 0
while r < len(nums):
n = nums[r]
# Maintain minq as increasing deque
while minq and n < nums[minq[-1]]:
minq.pop()
minq.append(r)
# Maintain maxq as decreasing deque
while maxq and n > nums[maxq[-1]]:
maxq.pop()
maxq.append(r)
r += 1
# Shrink window if condition fails
while l < r and nums[minq[0]] * 2 <= nums[maxq[0]]:
if minq[0] == l:
minq.popleft()
if maxq[0] == l:
maxq.popleft()
l += 1
ret = max(ret, r - l)
return ret
# Test the function
nums = [10, 2, 6, 6, 4, 4]
result = find_longest_sublist(nums)
print(f"Length of longest valid sublist: {result}")
Length of longest valid sublist: 4
How It Works
Let's trace through the example [10, 2, 6, 6, 4, 4] ?
from collections import deque
def find_longest_sublist_debug(nums):
ret = 0
minq, maxq = deque(), deque()
l, r = 0, 0
print(f"Input: {nums}")
print("Step by step:")
while r < len(nums):
n = nums[r]
print(f"\nProcessing nums[{r}] = {n}")
# Maintain deques
while minq and n < nums[minq[-1]]:
minq.pop()
minq.append(r)
while maxq and n > nums[maxq[-1]]:
maxq.pop()
maxq.append(r)
r += 1
# Check and shrink window
while l < r and nums[minq[0]] * 2 <= nums[maxq[0]]:
print(f" Condition fails: 2*{nums[minq[0]]} <= {nums[maxq[0]]}")
if minq[0] == l:
minq.popleft()
if maxq[0] == l:
maxq.popleft()
l += 1
current_length = r - l
ret = max(ret, current_length)
print(f" Window [{l}:{r}], length: {current_length}, max so far: {ret}")
return ret
nums = [10, 2, 6, 6, 4, 4]
find_longest_sublist_debug(nums)
Input: [10, 2, 6, 6, 4, 4] Step by step: Processing nums[0] = 10 Window [0:1], length: 1, max so far: 1 Processing nums[1] = 2 Window [0:2], length: 2, max so far: 2 Processing nums[2] = 6 Condition fails: 2*2 <= 10 Window [1:3], length: 2, max so far: 2 Processing nums[3] = 6 Window [1:4], length: 3, max so far: 3 Processing nums[4] = 4 Window [1:5], length: 4, max so far: 4 Processing nums[5] = 4 Window [1:6], length: 5, max so far: 5
Key Points
Deques maintain indices of elements in monotonic order for O(1) min/max queries
Sliding window expands when condition is satisfied and shrinks when it fails
Time complexity: O(n) as each element is added and removed from deques at most once
Space complexity: O(n) for the deques
Conclusion
This sliding window approach with monotonic deques efficiently finds the longest sublist where twice the minimum exceeds the maximum. The algorithm runs in O(n) time by maintaining the min/max values as the window expands and contracts.
