Java Merge Intervals Template Copy
This problem is a carbon copy of #56 Merge Intervals https://leetcode.com/problems/merge-intervals/submissions/1207690070/\n\nIn the original problem template, 
This problem is a carbon copy of #56 Merge Intervals https://leetcode.com/problems/merge-intervals/submissions/1207690070/\n\nIn the original problem template, we keep 2 ptrs to intervals `prev` and `curr` and we either `merge` them into one if they are `overlapping`, or add them to a dynamic list of `non-overlapping` intervals\nThe math behind is really simple, if the `prev` ends before `curr` starts, `ADD` prev to list, else `MERGE` curr into prev. Move curr as part of loop\nTranslating this to code is also fairly straightforward, and actually forms the core of pretty much 90% of interval problems if you followed the template. (logic behind `MIN`, `MAX` for `Union` and `Intersect` or the logic to compare `prev` vs `curr` to determine overlapability)\n\nFor this problem, all that changes is the way we `count` the number of `non-overlapping` intervals. Thus, we don\'t really need the dynamic list of non-overlapping intervals, we can simply `count` them at the time when we `add` to the list!\n\nFurthermore, if the problem were to simply ask for a regular count of non-overlapping intervals, we would do `count++` starting with `count = 1` (or start with `count = 0` and do 1 final increment outside of loop where we add the final prev)\n\nHowever, this problem asks to count in a "SPECIAL" way, and the special way being simply " `count = (count * 2) % mod`\n\nThats all, we can also start with `count = 2^1` = `count = 2` and `return count` in the end\n\n```\nclass Solution {\n    public int countWays(int[][] ranges) {\n        int n = ranges.length;\n        Arrays.sort(ranges, (a, b) -> a[0] - b[0]);\n        int[] prev = ranges[0];\n        int count = 2;\n        int mod = 1_000_000_007;\n        for (int i = 1; i < n; i++) {\n            int[] curr = ranges[i];\n            if (prev[1] < curr[0]) {\n                prev = curr;\n                count = (count * 2) % mod;\n            }\n            else {\n                prev[0] = Math.min(prev[0], curr[0]);\n                prev[1] = Math.max(prev[1], curr[1]);\n            }\n        }\n        return count;\n    }\n}\n```
['Java']