1096. Maximum Sum of Two Non-Overlapping Subarrays
My accepted Python solution to LeetCode problem 1096, Maximum Sum of Two Non-Overlapping Subarrays, running in 0ms.
- Difficulty: Medium
- Python
- Runtime 0ms
- Memory 17.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.4MB, accepted 2026-01-02.
class Solution:
def maxSumTwoNoOverlap(self, nums: List[int], firstLen: int, secondLen: int) -> int:
# Compute prefix sums
n = len(nums)
prefix = [0] * (n + 1)
for i in range(n):
prefix[i + 1] = prefix[i] + nums[i]
def getSum(i, j): # sum of nums[i:j]
return prefix[j] - prefix[i]
def maxSum(L, M): # L subarray comes first
result = 0
max_L = 0
for i in range(L + M, n + 1):
# L subarray ends at i-M, M subarray ends at i
max_L = max(max_L, getSum(i - M - L, i - M))
result = max(result, max_L + getSum(i - M, i))
return result
return max(maxSum(firstLen, secondLen), maxSum(secondLen, firstLen))