[Java] Using TreeMap to find the minimum interval
I\'ve noticed people are placing solutions to avoid TLE taking only the first interval. It usually fails on the next counter case:\n\n[1,2,3,3,3]\n6\n\n\nI\'ve 
I\'ve noticed people are placing solutions to avoid TLE taking only the first interval. It usually fails on the next counter case:\n```\n[1,2,3,3,3]\n6\n```\n\nI\'ve used a `TreeMap` to calculate the minimum interval prior to the current point.\n\n```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        int i = 0;\n        int j = 0;\n        int sum = 0;\n        int N = arr.length;\n        TreeMap<Integer, Integer> m = new TreeMap<>();\n        int best = -1;\n        while (i < N) {\n            if (j < N && sum < target) sum += arr[j++];\n            if (sum == target) {\n                m.put(j, i);\n\t\t\t\t// if we have another interval prior to this point, use it \n                Integer k = m.floorKey(i);\n                if (k != null) {\n                    int l = j - i + k - m.get(k);\n                    best = best == -1 ? l : Math.min(l, best);\n                }\n\t\t\t\t// put the best interval distance back, we check the best interval prior to this point, and if it has shorted distance - use it instead, so maintain the shortest distance for the left interval\n                Integer b = m.floorKey(j - 1);\n                int bl = b == null ? j - i : Math.min(j - i, b - m.get(b));\n                m.put(j, j - bl);\n            }\n            if (sum >= target) {\n                sum -= arr[i++];\n            }\n            \n            if (sum < target && j >= N) break;\n        }\n        return best;\n    }\n}\n```
['Java']