[Swift] DP solution
\n    func maxTaxiEarnings(_ n: Int, _ rides: [[Int]]) -> Int {\n        var memo = Array(repeating: 0, count: n + 1)\n        var ridesByEndpoint = [Int: [[Int
```\n    func maxTaxiEarnings(_ n: Int, _ rides: [[Int]]) -> Int {\n        var memo = Array(repeating: 0, count: n + 1)\n        var ridesByEndpoint = [Int: [[Int]]]()\n                    \n        for ride in rides {\n            ridesByEndpoint[ride[1], default: []].append(ride)\n        }\n        \n        for distance in 1...n {\n            memo[distance] = memo[distance - 1]\n            \n            guard let rides = ridesByEndpoint[distance] else { continue }\n            \n            for i in 0..<rides.count {\n                let ride = rides[i]\n                let money = ride[1] - ride[0] + ride[2]\n            \n                memo[distance] = max(memo[ride[0]] + money, memo[distance])\n            }\n        }\n        \n        return memo.last!\n    }\n```
['Dynamic Programming', 'Swift']