[Java] Sliding window and check overlap interval
Modify the code to handle failed cases\n\n\nBasica idea is using sliding window and check for the intervals sum to target.\nBut we may face some interval are ov
**Modify the code to handle failed cases**\n\n\nBasica idea is using sliding window and check for the intervals sum to target.\nBut we may face some interval are overlapped. \nSo we could matain the heap to check for overlapped intervals and select two non-overlapped interval that has least range.\n\n\n\n\n```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        int left = 0;\n        int sum = 0;\n        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);\n        for (int right = 0; right < arr.length; right++) {\n            sum += arr[right];         \n            while (sum > target) {\n                sum -= arr[left];\n                left++;\n            }\n            if (sum == target) {\n                int[] interval = {left, right, right - left + 1};\n                pq.offer(interval);\n            }\n        }\n        \n        if (pq.size() < 2) {\n            return -1;\n        }\n \n\n        int res = Integer.MAX_VALUE;\n        int[] first = pq.poll();\n        PriorityQueue<int[]> conflictedPq = new PriorityQueue<>((a, b) -> a[2] - b[2]);\n        \n        while (!pq.isEmpty()) {\n            int s = first[2];\n            int[] cur = pq.poll();\n\n            //check overlap, non overlap, possible solution;\n            boolean find = false;\n            if (cur[0] > first[1] || cur[1] < first[0]) {\n                s += cur[2];\n                res = Math.min(res, s);\n                find = true;\n            }\n            else {\n                // overlapped, those overlapped conflict with the selected smaller range, could also form possible solutions\n                conflictedPq.offer(cur);\n            }\n            \n            if (find || pq.isEmpty()) {\n                pq = conflictedPq;\n                conflictedPq = new PriorityQueue<>((a, b) -> a[2] - b[2]);\n                first = pq.poll();\n            }\n\n        }\n        return res == Integer.MAX_VALUE ? -1 : res;\n    }\n}\n```
[]