Problema
With inheritance, a subclass reuses its parent's behaviour and adds its own. Every vehicle pays a toll; a truck is a vehicle that pays extra for its axles.
Each vehicle is one of:
motorbike: pays 2.car: pays 5.truck <axles>: pays the car rate 5, plus 3 for every axle beyond the first two.
Implement vehicle_tolls(vehicles).
Input
vehicles: The vehicles passing the toll gate.
Output
Return the total toll collected.
Vincoli
0 ≤ number of vehicles ≤ 100,000
2 ≤ axles ≤ 10
Esempi
Input:
vehicles = ["car", "motorbike", "truck 4"]
Output atteso:
expected = 18
Input:
std::vector<std::string> vehicles = {"car", "motorbike", "truck 4"};
Output atteso:
long long expected = 18;
Input:
string[] vehicles = new string[] {"car", "motorbike", "truck 4"};
Output atteso:
long expected = 18;
Spiegazione
5 + 2 + (5 + 3 × 2) = 18.
Input:
vehicles = ["truck 2", "truck 3"]
Output atteso:
expected = 13
Input:
std::vector<std::string> vehicles = {"truck 2", "truck 3"};
Output atteso:
long long expected = 13;
Input:
string[] vehicles = new string[] {"truck 2", "truck 3"};
Output atteso:
long expected = 13;
Spiegazione
A 2-axle truck pays just the car rate 5; a 3-axle truck pays 5 + 3 = 8. Total 13.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Start from the car rate of 5 and adjust per vehicle type.
Suggerimento 2
A truck pays extra only for axles beyond the first two.
Suggerimento 3
Motorbikes override the base rule with a fixed 2.
Complessità attesa
O(number of vehicles) time, O(1) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.