JAVA-Unique Solution Using ArrayList - Passed 68/72 TestCases
This is my approach to this question. Approach is quite unique and passed 68/72 testcases. \nThis approach is not perfect as I am getting TLE, This is just for 
This is my approach to this question. Approach is quite unique and passed 68/72 testcases. \nThis approach is not perfect as I am getting TLE, This is just for the learning purpose. \n\n***UPVOTE  if you find it useful. ***\n\n**Approach is simple:**\n1. Fill the ArrayList "Big" whenever add() is called.\n2. Sort the ArrayList "Big" based on the left interval and count the numbers.\n\n```\nclass CountIntervals {\n     ArrayList<ArrayList<Integer>> big;\n    \n    public CountIntervals() {\n        big=new ArrayList<>();\n        \n    }\n    \n    public void add(int left, int right) {\n        ArrayList<Integer> small=new ArrayList<>();\n        small.add(left);\n        small.add(right);\n        \n        big.add(small);\n    }\n    \n    public int count() {\n        //Each Time sorting the intervals whenever count() is called\n        Collections.sort(big,(a,b)->Integer.compare(a.get(0),b.get(0)));\n        \n        int prevStart=0;\n        int prevEnd=0;\n        \n        int i=0;\n        int count=0;\n        \n        while(i<big.size())\n        {\n            \n            int newStart=big.get(i).get(0);\n            int newEnd=big.get(i).get(1);\n            \n            if(i==0)\n            {\n                count=count+newEnd-newStart+1;\n                prevStart=newStart;\n                prevEnd=newEnd;\n            }\n            \n            else\n            {\n                if(newStart==prevStart)\n                {\n                    if(newEnd>prevEnd)\n                    {\n                        count=count+newEnd-prevEnd;\n                    }\n                }\n                \n                else if(newStart>prevEnd)\n                {\n                    count=count+newEnd-newStart+1;\n                }\n                \n                else if(newStart<prevEnd)\n                {\n                    if(newEnd>prevEnd)\n                    count=count+newEnd-prevEnd;\n                }\n                \n                else if(newStart==prevEnd)\n                {\n                    count=count+newEnd-prevEnd;\n                }\n                \n                \n                prevStart=newStart;\n                \n                if(newEnd>prevEnd)\n                prevEnd=newEnd;\n            }\n            \n            i++;\n        }\n        \n        return count;\n    }\n}\n```\n
['Java']