431. Train Route Fare Calculator
Asked in
Train Route Fare Calculator

Design a train route fare calculator for one linear route.

Stops are added to the route in travel order. Travelling across each segment between two neighboring stops costs 1.

On weekends, the fare for a journey cannot exceed the configured weekend fare cap.

Fare Rules

  • Every segment between two neighboring stops costs 1.
  • Travel is allowed in either direction along the route.
  • The normal fare is the number of segments between startStop and endStop.
  • For a weekday journey, return the normal fare.
  • For a weekend journey, return the smaller of the normal fare and weekendFareCap.
  • A journey that starts and ends at the same stop costs 0.
  • The weekend cap applies separately to each call to calculateFare.

Class

TrainRouteFareCalculator

Constructor

TrainRouteFareCalculator

TrainRouteFareCalculator(int weekendFareCap)

  • Creates an empty train route.
  • weekendFareCap is the maximum fare charged for one weekend journey.

Methods

addStop

void addStop(String stopName)

  • Adds stopName to the end of the route.
  • The order of the addStop calls determines the order of the stops.

calculateFare

int calculateFare(String startStop, String endStop, boolean isWeekend)

  • startStop is the stop where the journey begins.
  • endStop is the stop where the journey ends.
  • isWeekend is true for a weekend journey and false for a weekday journey.
  • Returns the fare for the journey.

Constraints

  • 1 ≤ weekendFareCap ≤ 100,000
  • 1 ≤ stopName.length() ≤ 100
  • At most 100,000 stops are added.
  • At most 100,000 fare calculations are requested.
  • Every stop name is unique within the route.
  • Stop names contain only English letters, digits, and hyphens.
  • Every startStop and endStop passed to calculateFare has already been added.
  • No parameter value is null.

Example 1

TrainRouteFareCalculator calculator = new TrainRouteFareCalculator(weekendFareCap = 3);

calculator.addStop(stopName = "Cedar");

calculator.addStop(stopName = "Lake");

calculator.addStop(stopName = "Museum");

calculator.addStop(stopName = "Market");

calculator.addStop(stopName = "Airport");

calculator.addStop(stopName = "Harbor");

calculator.calculateFare( startStop = "Cedar", endStop = "Harbor", isWeekend = false )

Output: 5

The journey crosses five segments. Because it is a weekday journey, the weekend cap does not apply.

Example 2

Using the same calculator and route:

calculator.calculateFare( startStop = "Cedar", endStop = "Harbor", isWeekend = true )

Output: 3

The normal fare is 5, but weekend fares are capped at 3.

Example 3

Using the same calculator and route:

calculator.calculateFare( startStop = "Market", endStop = "Lake", isWeekend = true )

Output: 2

The stops are two segments apart. The normal fare is already below the weekend cap.

Example 4

Using the same calculator and route:

calculator.calculateFare( startStop = "Museum", endStop = "Museum", isWeekend = false )

Output: 0

No segment is travelled when both stops are the same.



Please use Laptop/Desktop or any other large screen to add/edit code.