<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>

class Solution:
    def minSumOfLengths(self, arr: List[int], target: int) -> int:
        # We keep track of the running length of the best solution to here, from both
        # the right and the left direction.
        # Each direction encompasses the best window for each of the two.
        # THen, we ensure no overlap by checking indices off by 1.
        def min_to_here(arr):
            running_sum = 0
            best_to_here = [float('inf')]  # off by 1.
            lookup = collections.defaultdict(int)

            for i, num in enumerate(arr, 1):
                running_sum += num
                # All smaller windows eill be caught by case 2 below.
                curr_best = float('inf')
                if running_sum == target:  # success
                    curr_best = i
                elif running_sum - target in lookup:  # Overshot, but we could bring window in.
                    curr_best = i - lookup[running_sum - target]
                else:
                    curr_best = float('inf')  # Didn't work.
                lookup[running_sum] = i  # This sum has this window size. Will always be largest by construction of my setup (i increasing) which is desired.
                best_to_here.append(min(best_to_here[i - 1], curr_best))
            return best_to_here[1:]
        
        from_left = min_to_here(arr)
        from_right = min_to_here(arr[::-1])[::-1]  # Need reversed, but in correct indexing

        min_sum = float('inf')
        for overlap in range(1, len(arr)):
            min_sum = min(min_sum, from_left[overlap-1] + from_right[overlap])
        if min_sum == float('inf'):
            return -1
        else:
            return min_sum
40100