TreeMap (SortedMap solution)
\nimport java.util.Optional;\n\nclass Solution {\n    public int maxTwoEvents(int[][] events) {\n        // 1. sort by starting time. O(nlogn)\n        // 2. ma
```\nimport java.util.Optional;\n\nclass Solution {\n    public int maxTwoEvents(int[][] events) {\n        // 1. sort by starting time. O(nlogn)\n        // 2. map of x -> y meaning for all events starting >=x, y=max value . O(n)\n        // 3. iterate again, for each end position, find map.get(end+1).\n        Arrays.sort(events, (int[] e1, int[] e2) ->{\n            return Integer.compare(e1[0],e2[0]);\n        });\n        TreeMap<Integer, Integer> navMap = \n            new TreeMap<Integer, Integer>();\n        int max = Integer.MIN_VALUE;\n        for (int i=events.length-1; i>=0; i--) {\n            max = Integer.max(max, events[i][2]);\n            navMap.put(events[i][0], max);\n        }\n        \n        // 3.\n        int res = 0;\n        for (int[] event : events) {\n            res = Integer.max(res, event[2] + Optional.ofNullable(navMap.ceilingEntry(event[1] +1)) \n                              .map(e->e.getValue() ).orElse(0)  );\n        }\n        return res;\n    }\n}\n```
[]