Sort and Bins
Intuition\nFirst i looked at hashing start indicies, but remembered that sorting allows for a greedy reduction of groups. The rest of this problem is just combi
# Intuition\nFirst i looked at hashing start indicies, but remembered that sorting allows for a greedy reduction of groups. The rest of this problem is just combinations which can be just computed with the sum of $$2^n$$. First I was looking at computing combinations using $$n!/(k!*(n-k)!)$$, but it was expensive to maintain the factorial and module logic.\n\n# Approach\n* Sort by the start\n* Greed/DP algorithm to merge groups\n* Find bin combinations of the number of groups.\n\n# Complexity\n- Time complexity:\n$$O(N)$$\n\n- Space complexity:\n$$O(N)$$\n\n# Code\n```\nfunction countWays(ranges: number[][]): number {\n    const groups = []\n    const sorted = ranges.sort((a,b) => a[0] - b[0]);\n    for(const range of sorted) {\n        if(groups.length === 0) {\n            groups.push(range);\n        }\n        const [start, end] = range;\n        const [lastStart, lastEnd] = groups[groups.length - 1] ?? [0, 0];\n        if(start <= lastEnd) {\n            groups[groups.length - 1] = [lastStart, Math.max(end, lastEnd)];\n        } else {\n            groups.push(range);\n        }\n    }\n    return countBinCombinationsMod(groups.length, 1000000007);\n};\n\nfunction countBinCombinationsMod(n: number, mod: number) {\n    let result = 1;\n    for (let i = 0; i < n; i++) {\n        result = (result * 2) % mod;\n    }\n    return result;\n}\n```
['TypeScript']