<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:
        if len(arr)<=1:
            return -1

        #Find all subarrays
        def findAllsubarrays(arr_tmp):
            start_pt = 0
            end_pt = 0
            curSum = arr_tmp[0]
            all_subarrays = []
            while start_pt<len(arr_tmp):
                if start_pt>end_pt:
                    end_pt=start_pt
                    curSum = arr_tmp[start_pt]
                if curSum == target:
                    all_subarrays.append(end_pt-start_pt+1)
                    curSum-=arr_tmp[start_pt]
                    start_pt+=1
                elif curSum<target:
                    end_pt+=1
                    if end_pt<len(arr_tmp):
                        curSum+=arr_tmp[end_pt]
                    else:
                        all_subarrays.append(1e5+1)
                        curSum-=arr_tmp[start_pt]
                        start_pt+=1
                else:
                    all_subarrays.append(1e5+1)
                    curSum-=arr_tmp[start_pt]
                    start_pt+=1
                # print(all_subarrays)
            return all_subarrays

        suffix_cand = findAllsubarrays(arr)
        prefix_cand = findAllsubarrays(arr[::-1])
        # print(suffix_cand)
        # print(prefix_cand)

        

        #Do dynamic programming use prefix and suffix
        min_suffix = []
        n=len(arr)
        for i in range(n-1,0,-1):
            if i==n-1:
                min_suffix.append(suffix_cand[i])
            else:
                the_min = min(min_suffix[-1],suffix_cand[i])
                min_suffix.append(the_min)
        min_suffix = min_suffix[::-1]

        min_prefix = []
        for i in range(n-1,0,-1):
            if i==n-1:
                min_prefix.append(prefix_cand[i])
            else:
                the_min = min(min_prefix[-1],prefix_cand[i])
                min_prefix.append(the_min)

        # print(min_suffix)
        # print(min_prefix)
        results = [pre+sur for pre, sur in zip(min_prefix,min_suffix)]
        out = min(results)
        if out > 1e5:
            return -1
        else:
            return out
        
31400