prefix sum + sliding window - python code with comments beat 99%
\nclass Solution(object):\n    def minSumOfLengths(self, arr, target):\n        # prefix sum + sliding window method O(n)\n        \n        n = len(arr)\n     
```\nclass Solution(object):\n    def minSumOfLengths(self, arr, target):\n        # prefix sum + sliding window method O(n)\n        \n        n = len(arr)\n        Max = 2**31-1\n        minlens = [Max] * n # every position, has a min-length subarray ends/or before it, with a sum-up = target\n        minlen = Max # currently (global) min-length subarray and whose sum-up = target\n        ans = Max # final answer\n        \n        left = 0\n        presum = 0  # store prefix-sum\n        \n        for right in range(n):\n            presum += arr[right]\n            \n            while presum > target: # sliding window method to enlarge left so that presum <= target\n                presum -= arr[left]\n                left += 1\n            \n            if presum == target: # update values when we find presum==target\n                curlen = right - left + 1 # the length of current subarray from left to right, both inclusive [left, right]\n                if left > 0 and minlens[left-1] < Max: \n\t\t\t\t    # since left is included in current subarray, we have to find a subarray with sum=target before left\n\t\t\t\t    # and the best is recorded in minles[left-1]:\n                    ans = min(ans, curlen + minlens[left-1]) \n\t\t\t\t\t# update ans, a former subarray with length minlens[left-1] and current length\n                minlen = min(minlen, curlen) # update the global min-length of subarray whose sum-up = target\n            minlens[right] = minlen # update right-position\'s best minlen(before or ends at \'right\')\n        return -1 if ans == Max else ans\n```
[]