<p>You are given an array of integers <code>arr</code> and an integer <code>target</code>.</p>

<p>You have to find <strong>two non-overlapping sub-arrays</strong> of <code>arr</code> each with a sum equal <code>target</code>. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is <strong>minimum</strong>.</p>

<p>Return <em>the minimum sum of the lengths</em> of the two required sub-arrays, or return <code>-1</code> if you cannot find such two sub-arrays.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,2,2,4,3], target = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [7,3,4,7], target = 7
<strong>Output:</strong> 2
<strong>Explanation:</strong> Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [4,3,2,6,2,3,4], target = 6
<strong>Output:</strong> -1
<strong>Explanation:</strong> We have only one sub-array of sum = 6.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arr[i] &lt;= 1000</code></li>
	<li><code>1 &lt;= target &lt;= 10<sup>8</sup></code></li>
</ul>

import math
class Solution:
    def minSumOfLengths(self, arr: List[int], target: int) -> int:
        # Sliding window
        # Time complexity: O(n), space complexity: O(n).
        n = len(arr)
        # Find all subarrays that sum up to cnt and store their
        # locations.
        starts, ends = dict(), dict()
        start, cnt = 0, 0
        for end, x in enumerate(arr):
            # The current subarray is arr[start:(end + 1)] whose
            # sum is cnt.
            cnt += x
            while cnt > target:
                cnt -= arr[start]
                start += 1
            if cnt == target:
                ends[start] = end
                starts[end] = start
        
        # rlths[i] is the minimum length of subarrays of arr[i:]
        # that sum up to cnt.
        rlths = [math.inf for _ in range(n + 1)]
        for i in reversed(range(n)):
            if i in ends:
                l = ends[i] - i + 1
                rlths[i] = min(rlths[i + 1], l)
            else:
                rlths[i] = rlths[i + 1]
        
        llth, ans = math.inf, math.inf
        for i in range(n):
        # At the beginning of the i-th iteration, llth is the
        # minimum length of subarrays of arr[:i] that sum up to
        # cnt.
            if i in starts:
                l = i - starts[i] + 1
                llth = min(llth, l)
                ans = min(ans, llth + rlths[i + 1])
        return ans if ans < math.inf else -1
46500