Simple Java solution with explanation - Sort + Heap
This problem can be solved with the help of heap.\n\n- First sort all events by start time. If start time of two events are equal, sort them by end time.\n- The
This problem can be solved with the help of `heap`.\n\n- First sort all events by start time. If start time of two events are equal, sort them by end time.\n- Then take a priority queue that takes an array containing `[endtime, value]`. Priority queue will sort elements on the basis of `end time`.\n- Iterate through events, for each event `e`, calculate maximum value from all events that ends before `e[0]` (i.e. start time). Let\'s store this value in `maxVal` variable.\n- Now answer will be `ans = max(ans, e[2] + maxVal)`.\n\n```java\nclass Solution {\n    public int maxTwoEvents(int[][] events) {\n        int n = events.length;\n        Arrays.sort(events, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);\n        PriorityQueue<int[]> queue = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);\n        \n        int maxVal = 0, ans = 0;\n        for(int[] e : events){            \n            int start = e[0];\n            while(!queue.isEmpty()){\n                if(queue.peek()[0] >= start)\n                    break;\n                int[] eve = queue.remove();\n                maxVal = Math.max(maxVal, eve[1]);\n            }\n            ans = Math.max(ans, e[2] + maxVal);\n            queue.add(new int[]{e[1], e[2]});\n        }\n        \n        return ans;\n    }\n}\n```\n\n**Note: If you have any doubt, please ask question, I\'ll be happpy to answer.\nIf you like solution, please upvote**
[]