Scala
\nobject Solution {\n  def maxTaxiEarnings(n: Int, rides: Array[Array[Int]]): Long = {\n    val startToEndAndTip = rides.groupMap(_(0)) { case Array(_, end, tip
```\nobject Solution {\n  def maxTaxiEarnings(n: Int, rides: Array[Array[Int]]): Long = {\n    val startToEndAndTip = rides.groupMap(_(0)) { case Array(_, end, tip) => (end, tip) }.withDefaultValue(Array.empty)\n    (1 until n)\n      .foldRight(IndexedSeq(0L)) {\n        case (start, dp) => dp.prepended {\n          Iterator(dp(0))\n            .concat {\n              startToEndAndTip(start).iterator.map {\n                case (end, tip) =>\n                  val distance = end - start\n                  (distance + tip) + dp(distance - 1)\n              }\n            }\n            .max\n        }\n      }\n      .head\n  }\n}\n```
['Scala']