size = len(nums) if total == x: return size count = collections.defaultdict(int) temp = 0 for i inrange(size): temp += nums[i] count[i] = temp
target = total - x start = 0 result = size for end inrange(size): whileTrue: value = count[end] - count[start - 1] if value == target: result = min(result, start + (size - end - 1)) break elif value < target: break start += 1 if start > end: break if result == size: return -1 return result