Ctrl K

Shuttle Driver Earnings

Medium
A driver travels along a road with stops numbered 1 to stops, always in one direction, from lower numbers to higher ones. There are orders orders, where orders[i] = [from, to, bonus] means a passenger wants a ride from stop from to stop to and will pay an extra bonus. Each passenger carried earns the driver to - from + bonus. Only one passenger can ride at a time, but the driver may drop someone off and pick up the next passenger at the same stop. Return the most money the driver can earn by choosing orders wisely.
Example 1
stops=8, orders=[[1, 4, 2], [3, 8, 1], [4, 6, 5]] → 12

Carry the first passenger from 1 to 4 (3 + 2 = 5), then at stop 4 pick up the third one to 6 (2 + 5 = 7), for 12 in total. The second order (5 + 1 = 6) overlaps both and does not pay off.

Example 2
stops=10, orders=[[1, 10, 1], [2, 3, 1], [5, 7, 1]] → 10

The long ride pays 9 + 1 = 10, while the two short ones together pay 2 + 3 = 5, so the long one wins.

Constraints
  • 1 ≤ stops ≤ 10⁵
  • 1 ≤ orders.length ≤ 3 · 10⁴
  • orders[i].length = 3
  • 1 ≤ from < to ≤ stops
  • 1 ≤ bonus ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords Hidden in a Text MediumSquaring Ladder MediumSell Plots to Bidders MediumSpells Without Neighbors MediumClimbing Along Rows and Columns Hard
loading Python…
Input
stops=8, orders=[[1, 4, 2], [3, 8, 1], [4, 6, 5]]
Expected
12

Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.