[Java] DP and window sliding, one pass O(n) time, easy to understand
Borrow the idea from the TreeMap solution. dp[i] stores the length of shortest interval with sum equals to target from index 0 to i. Using slide window to calcu
Borrow the idea from the TreeMap solution. dp[i] stores the length of shortest interval with sum equals to target from index 0 to i. Using slide window to calculate sum of subarray, whenever target is met, use dp to find the min interval not overlapping. At the end of each loop, update dp[i] = min(dp[i], dp[i-1]).\n\n```\npublic int minSumOfLengths(int[] arr, int target) {\n        int n = arr.length;\n        int[] dp = new int[n];\n        Arrays.fill(dp, Integer.MAX_VALUE);\n        int sum = 0;\n        int i = 0;\n        int j = 0;\n        int res = Integer.MAX_VALUE;\n        while(j < arr.length)\n        {\n            sum += arr[j];\n            while(sum > target)\n            {\n                sum -= arr[i];\n                i++;\n            }\n            if(sum == target)\n            {\n                if(i-1 >= 0 && dp[i-1] != Integer.MAX_VALUE)\n                {\n                    res = Math.min(res, j-i+1 + dp[i-1]); //find non overlap interval before i\n                }\n                dp[j] = j-i+1;\n            }\n            if(j > 0)\n                dp[j] = Math.min(dp[j], dp[j-1]); //the min interval until j\n            j++;\n        }\n        return res == Integer.MAX_VALUE ? -1 : res;\n    }\n\t
[]