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 maximum sum of two non-overlapping sublists in Python
Finding the maximum sum of two non-overlapping sublists is a classic dynamic programming problem. Given a list of numbers and two lengths x and y, we need to find two sublists with these lengths that don't overlap and have the maximum combined sum.
So, if the input is like nums = [3, 2, 10, -2, 7, 6], x = 3, y = 1, then the output will be 22, as the sublist with length 3 we select [3, 2, 10] (sum = 15) and for the other we select [7] (sum = 7).
Algorithm Steps
To solve this, we will follow these steps ?
- Create a prefix sum array to calculate sublist sums efficiently
- For each possible arrangement (x then y, or y then x), find the maximum sum
- Use prefix maximum to track the best sublist sum seen so far
- Return the maximum of both arrangements
Implementation
class Solution:
def solve(self, nums, len1, len2):
# Create prefix sum array
prefix_sum = [0]
for x in nums:
prefix_sum.append(prefix_sum[-1] + x)
def find_max_sum(first_len, second_len):
# Get all possible sums for sublists of first_len
sublist_sums = [prefix_sum[i + first_len] - prefix_sum[i]
for i in range(len(prefix_sum) - first_len)]
# Create prefix maximum array
prefix_max = sublist_sums[:]
for i in range(len(prefix_max) - 1):
prefix_max[i + 1] = max(prefix_max[i + 1], prefix_max[i])
max_sum = float("-inf")
# Try placing second sublist after first sublist
for i in range(first_len, len(prefix_sum) - second_len):
left_max = prefix_max[i - first_len]
right_sum = prefix_sum[i + second_len] - prefix_sum[i]
max_sum = max(max_sum, left_max + right_sum)
return max_sum
# Try both arrangements: (len1, len2) and (len2, len1)
return max(find_max_sum(len1, len2), find_max_sum(len2, len1))
# Example usage
ob = Solution()
nums = [3, 2, 10, -2, 7, 6]
x = 3
y = 1
result = ob.solve(nums, x, y)
print(f"Maximum sum of two non-overlapping sublists: {result}")
The output of the above code is ?
Maximum sum of two non-overlapping sublists: 22
How It Works
The algorithm uses prefix sums for efficient sublist sum calculation and prefix maximum arrays to track the best sublist seen so far. Here's the breakdown:
- Prefix Sum: Allows O(1) calculation of any sublist sum
- Two Arrangements: We try placing the first sublist before the second, and vice versa
- Prefix Maximum: For each position, we know the maximum sublist sum that can appear before it
- Non-overlapping Constraint: Ensured by proper indexing in the loops
Example Walkthrough
For nums = [3, 2, 10, -2, 7, 6] with x = 3, y = 1:
nums = [3, 2, 10, -2, 7, 6]
x, y = 3, 1
# Possible sublists of length 3:
# [3, 2, 10] = 15, [2, 10, -2] = 10, [-2, 7, 6] = 11, [10, -2, 7] = 15
# Possible sublists of length 1:
# [3] = 3, [2] = 2, [10] = 10, [-2] = -2, [7] = 7, [6] = 6
# Best combination: [3, 2, 10] (15) + [7] (7) = 22
print("Best combination gives sum:", 15 + 7)
Best combination gives sum: 22
Time Complexity
The time complexity is O(n) where n is the length of the input array. The space complexity is also O(n) for the prefix sum and prefix maximum arrays.
Conclusion
This solution efficiently finds the maximum sum of two non-overlapping sublists using prefix sums and dynamic programming. The key insight is trying both possible arrangements and using prefix maximum arrays to track the best sublist sum at each position.
