Python - Sliding Window - O(n) with detail comments.
\n```python\nclass Solution:\n    def minSumOfLengths(self, arr: List[int], target: int) -> int:\n        l, windowSum, res = 0, 0, float(\'inf\')\n        min_
\n```python\nclass Solution:\n    def minSumOfLengths(self, arr: List[int], target: int) -> int:\n        l, windowSum, res = 0, 0, float(\'inf\')\n        min_till = [float(\'inf\')] * len(arr) # records smallest lenth of subarry with target sum up till index i.\n        for r, num in enumerate(arr): # r:right pointer and index of num in arr\n            windowSum += num\n            while windowSum > target: \n\t\t\t# when the sum of current window is larger then target, shrink the left end of the window one by one until windowSum <= target\n                windowSum -= arr[l]\n                l += 1\n\t\t\t# the case when we found a new target sub-array, i.e. current window\n            if windowSum == target:\n\t\t\t   # length of current window\n                curLen = r - l + 1\n\t\t\t\t# min_till[l - 1]: the subarray with min len up till the previous position of left end of the current window: \n\t\t\t\t# avoid overlap with cur window\n\t\t\t\t# new_sum_of_two_subarray = length of current window + the previous min length of target subarray without overlapping\n\t\t\t\t# , if < res, update res.\n                res = min(res, curLen + min_till[l - 1])\n\t\t\t\t# Everytime we found a target window, update the min_till of current right end of the window, \n\t\t\t\t# for future use when sum up to new length of sum_of_two_subarray and update the res.\n                min_till[r] = min(curLen, min_till[r - 1])\n            else:\n\t\t\t# If windowSum < target: window with current arr[r] as right end does not have any target subarry, \n\t\t\t# the min_till[r] doesn\'t get any new minimum update, i.e it equals to previous min_till at index r - 1. \n                min_till[r] = min_till[r - 1]\n        return res if res < float(\'inf\') else -1\n\t\nTime = O(n): when sliding the window, left and right pointers traverse the array once.\nSpace = O(n): we use one additional list min_till[] to record min length of target subarray till index i.
['Sliding Window', 'Python', 'Python3']