Swift | Consider all the event points
Code\nswift []\nclass Solution {\n    func maxTwoEvents(_ events: [[Int]]) -> Int {\n        // Every event has 2 points: its start and its end.\n        enum E
# Code\n```swift []\nclass Solution {\n    func maxTwoEvents(_ events: [[Int]]) -> Int {\n        // Every event has 2 points: its start and its end.\n        enum EventType: Int { case start = 1, end = 0 }\n        typealias EventPoint = (type: EventType, time: Int, value: Int)\n\n        // Construct the different event points, sorting them by time.\n        let eventPoints = events\n            .reduce(into: [EventPoint]()) {\n                $0.append(EventPoint(type: .start, time: $1[0], value: $1[2]))\n                $0.append(EventPoint(type: .end, time: $1[1] + 1, value: $1[2]))\n            }\n            .sorted { ($0.time, $0.type.rawValue) < ($1.time, $1.type.rawValue) }\n\n        // Go through all the event points, updating the result.\n        var bestValue = 0\n        var res = 0\n        for eventPoint in eventPoints {\n            switch eventPoint.type {\n            case .start:\n                // If the event being started, combined with the best completed\n                // event so far, improves the result, update the result.\n                res = max(res, bestValue + eventPoint.value)\n            case .end:\n                // If the event just being completed is better than any\n                // previous event, update "bestValue".\n                bestValue = max(bestValue, eventPoint.value)\n            }\n        }\n        return res\n    }\n}\n```
['Swift']