[Python 3] very short O(N)
\nclass Solution:\n    def minSumOfLengths(self, a: List[int], targ: int) -> int:\n        \n        n = len(a)\n        d1 = defaultdict(lambda: inf)\n        
```\nclass Solution:\n    def minSumOfLengths(self, a: List[int], targ: int) -> int:\n        \n        n = len(a)\n        d1 = defaultdict(lambda: inf)\n        ret2 = inf\n        tot = 0\n        j = 0\n        \n        for i,x in enumerate(a):\n            \n            tot += x\n            \n            while tot > targ:\n                tot -= a[j]\n                j += 1\n            \n            sz = i-j+1\n            \n            if tot == targ:\n                d1[i] = sz\n                ret2 = min(ret2, d1[j-1] + sz)\n            \n            d1[i] = min(d1[i], d1[i-1])\n        \n        return -1 if ret2 == inf else ret2\n```
[]