O(n) time, O(n) space, a little more intuitive
class Solution:\n    def minSumOfLengths(self, arr: List[int], target: int) -> int:\n        \n        running_sum = 0\n        h = {}\n        h[0] = -1\n     
class Solution:\n    def minSumOfLengths(self, arr: List[int], target: int) -> int:\n        \n        running_sum = 0\n        h = {}\n        h[0] = -1\n        min_at_index = [float(inf)]*len(arr)\n        global_min = inf\n        for i,v in enumerate(arr):\n            \n            running_sum += v\n            \n            if running_sum - target in h.keys():\n                #inclusive start_index\n                start_index = h[running_sum - target] + 1\n                curr_length = i - start_index + 1\n                \n                global_min = min(curr_length + min_at_index[start_index-1],global_min)\n                min_at_index[i] = min(min_at_index[i-1],curr_length)\n            else:\n                min_at_index[i] = min_at_index[i - 1]\n                \n            h[running_sum] = i\n            \n        if global_min == inf:\n            return -1\n        \n        return global_min
[]