Summary for 3 ways by using prefix sum or sliding window
prefix sum and iterate twice\n\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) { //this fits the case when there\'s negative number, ki
prefix sum and iterate twice\n```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) { //this fits the case when there\'s negative number, kind like 560\n        if (arr == null || arr.length == 0) return 0;\n        Map<Integer, Integer> map = new HashMap<>(); //sum - index\n        map.put(0, -1);\n        int sum = 0;\n        for (int i = 0; i < arr.length; i++) { //record preSum and index\n            sum += arr[i];\n            map.put(sum, i);\n        }\n        sum = 0;\n        int size = arr.length + 1, res = arr.length + 1;//note if we set size as MAX_VALUE the line 16 may overflow\n        for (int i = 0; i < arr.length; i++) {\n            sum += arr[i];\n            if (map.containsKey(sum - target)) size = Math.min(size, i - map.get(sum - target)); //find the subarray from the previous index to current one\n            if (map.containsKey(sum + target)) res = Math.min(res, size + map.get(sum + target) - i); //from the current index to next one, this avoid overlap\n        }\n        return res == arr.length + 1 ? -1 : res;\n    }\n}\n```\n\nprefix sum just iterate once by using extra array to record previous valid subarray length\n```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        if (arr == null || arr.length == 0) return 0;\n        HashMap<Integer, Integer> map = new HashMap<>();\n        int sum = 0, n = arr.length, res = n + 1;\n        map.put(0, -1);\n        int[] min = new int[n];\n        for (int i = 0; i < arr.length; i++) {\n            sum += arr[i];\n            min[i] = i > 0 ? min[i - 1] : n + 1;\n            if (map.containsKey(sum - target)) {\n                int pre = map.get(sum - target);\n                min[i] = Math.min(min[i], i - pre);\n\t\t\t\t// if pre equals -1 means we only get one sub-array whoes sum eqauls target\n                if (pre != -1 && min[pre] != n + 1) res = Math.min(res, min[pre] + i - pre);\n            }\n            map.put(sum, i);\n        }\n        return res == n + 1 ? -1 : res;\n    }\n}\n```\n\nSliding window without hashmap to iterate once and also use extra array to record previous valid subarray length\nNote this only works when all elements >= 0\n```\nclass Solution { \n   public int minSumOfLengths(int[] arr, int target) {\n       if (arr == null || arr.length == 0) return 0;\n       int n = arr.length;\n       int[] best = new int[n];\n       Arrays.fill(best, n + 1);\n       int sum = 0, start = 0, ans = n + 1, bestSoFar = n + 1;\n       for(int i = 0; i < n; i++){\n           sum += arr[i];\n           while(sum > target) sum -= arr[start++];\n           if(sum == target){\n               int cur = i - start + 1;\n               if(start > 0 && best[start - 1] != n + 1) ans = Math.min(ans, best[start - 1] + cur); //first subarray exists\n               bestSoFar = Math.min(bestSoFar, cur); //update current one\n           }\n           best[i] = bestSoFar;\n       }\n       return ans == n + 1 ? -1 : ans;\n   }\n}
['Sliding Window', 'Prefix Sum', 'Java']