Python one-pass using defaultdict (instead of sliding window)
Like most solutions, I basically do this:\n(1) Maintain a prefix sum and store the last index for the current prefix sum ("lastSeen").\n(2) Use defaultdict ("mi
Like most solutions, I basically do this:\n(1) Maintain a prefix sum and store the last index for the current prefix sum ("lastSeen").\n(2) Use defaultdict ("minSize") to store the minimum size of the qualifying subarray up to that point.\n(3) Update these as well as the result in the loop. Others use the sliding window approach to avoid maintaining the "lastSeen" data structure but I find that harder to understand.\n\n```\nfrom collections import defaultdict\n\nclass Solution(object):\n    def minSumOfLengths(self, arr, target):\n        inf = float(\'inf\')\n        prefix = 0\n        minSize = defaultdict(lambda: inf)\n        lastSeen = {0: -1}\n        result = inf\n        for i, n in enumerate(arr):\n            prefix += n\n            if prefix-target in lastSeen:\n                j = lastSeen[prefix-target]\n                result = min(result, i-j + minSize[j])\n                minSize[i] = min(minSize[i-1], i-j)\n            else:\n                minSize[i] = minSize[i-1]\n            \n            lastSeen[prefix] = i\n\n        return -1 if result == inf else result\n```
[]