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 size of smallest sublist whose sum at least target in Python
Suppose we have a list of numbers called nums, and another input called target, we have to find the size of the shortest sublist such that its sum value is same as target or larger. If there is no such sublist then return -1.
So, if the input is like nums = [2, 11, -4, 17, 4] target = 19, then the output will be 2, as we can select [17, 4] to get sum of at least 19.
Algorithm Approach
To solve this, we will follow these steps ?
Create a prefix sum array
psstarting with [0]For each number in nums, add it to the last prefix sum and append to
psIf any single number ? target, return 1
Use a deque to maintain indices in increasing order of prefix sums
For each position, find the minimum subarray length that meets the target
Return the minimum size found, or -1 if none exists
Example
Let us see the following implementation to get better understanding ?
class Solution:
def solve(self, nums, target):
ps = [0]
for num in nums:
ps.append(ps[-1] + num)
if num >= target:
return 1
min_size = float("inf")
q = [0]
j = 0
for i in range(1, len(ps)):
j = min(j, len(q) - 1)
while j < len(q) and ps[i] - ps[q[j]] >= target:
min_size = min(min_size, i - q[j])
j += 1
while q and ps[i] <= ps[q[-1]]:
q.pop()
q.append(i)
return min_size if min_size < float("inf") else -1
# Test the solution
ob = Solution()
nums = [2, 11, -4, 17, 4]
target = 19
print(ob.solve(nums, target))
2
How It Works
The algorithm uses prefix sums and a monotonic deque:
Prefix sums: ps[i] contains sum of elements from index 0 to i-1
Subarray sum: Sum from index j to i-1 = ps[i] - ps[j]
Monotonic deque: Maintains indices with increasing prefix sum values
Early termination: If any single element ? target, return 1
Alternative Approach - Sliding Window
For arrays with positive numbers only, we can use a simpler sliding window approach ?
def min_subarray_len_positive(nums, target):
"""For positive numbers only - simpler sliding window"""
left = 0
current_sum = 0
min_len = float('inf')
for right in range(len(nums)):
current_sum += nums[right]
while current_sum >= target:
min_len = min(min_len, right - left + 1)
current_sum -= nums[left]
left += 1
return min_len if min_len != float('inf') else -1
# Test with positive numbers
positive_nums = [2, 1, 2, 4, 3, 1]
target = 7
print(min_subarray_len_positive(positive_nums, target))
2
Comparison
| Approach | Time Complexity | Space Complexity | Works With Negatives? |
|---|---|---|---|
| Prefix Sum + Deque | O(n) | O(n) | Yes |
| Sliding Window | O(n) | O(1) | No |
Conclusion
The prefix sum with monotonic deque approach efficiently handles arrays with negative numbers in O(n) time. For positive-only arrays, the sliding window technique is simpler and uses constant space.
