Java. Merge Intervals Using PriorityQueue + Permutation Helper
Code\n\nclass Solution {\n    static long MOD = (long)1E9 + 7;\n\n    public int countWays(int[][] ranges) {\n        Arrays.sort(ranges, (a, b) -> Integer.comp
# Code\n```\nclass Solution {\n    static long MOD = (long)1E9 + 7;\n\n    public int countWays(int[][] ranges) {\n        Arrays.sort(ranges, (a, b) -> Integer.compare(a[0], b[0]));\n\n        Queue<int[]> pq = new PriorityQueue<>((a, b) -> -Integer.compare(a[1], b[1]));\n        pq.offer(ranges[0]);\n\n        for (int i = 1; i < ranges.length; i++) {\n            int[] rangeWithLatestEnd = pq.peek();\n            if (rangeWithLatestEnd[1] >= ranges[i][0]) {\n                rangeWithLatestEnd = pq.poll();\n                rangeWithLatestEnd[1] = Math.max(rangeWithLatestEnd[1], ranges[i][1]);\n                pq.offer(rangeWithLatestEnd);\n            } else {\n                pq.offer(ranges[i]);\n            }\n        }\n\n        int nonOverlap = pq.size();\n\n        return permutation(nonOverlap, 2, new HashMap<>());\n\n    }\n\n    int permutation(int items, int slots, Map<Integer, Integer> cache) {\n        if (cache.containsKey(items)) {\n            return cache.get(items);\n        } else if (items < 0) {\n            return 0;\n        } else if (items == 0) {\n            return 1;\n        }\n        long ans = 0;\n        for (int i = 0; i < slots; i++) {\n            ans = (ans + permutation(items - 1, slots, cache)) % MOD;\n        }\n\n        cache.put(items, (int) ans);\n        return cache.get(items);\n    }\n}\n```
['Heap (Priority Queue)', 'Combinatorics', 'Java']