Facile 0 résolus 0 % de réussite

Vehicle Tolls

Charge tolls for motorbikes, cars and trucks where trucks extend the basic vehicle rule with axles.

Ce que vous allez apprendre

  • Reuse a parent's rule in a subclass
  • Add extra cost on top of a base rate
  • Pick the right class from a description

Problème

Afficher la traduction

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.

Contraintes

0 ≤ number of vehicles ≤ 100,000

2 ≤ axles ≤ 10

Exemples

Exemple 1
Entrée :
vehicles = ["car", "motorbike", "truck 4"]

Sortie attendue :
expected = 18
Entrée :
std::vector<std::string> vehicles = {"car", "motorbike", "truck 4"};

Sortie attendue :
long long expected = 18;
Entrée :
string[] vehicles = new string[] {"car", "motorbike", "truck 4"};

Sortie attendue :
long expected = 18;

Explication

5 + 2 + (5 + 3 × 2) = 18.

Exemple 2
Entrée :
vehicles = ["truck 2", "truck 3"]

Sortie attendue :
expected = 13
Entrée :
std::vector<std::string> vehicles = {"truck 2", "truck 3"};

Sortie attendue :
long long expected = 13;
Entrée :
string[] vehicles = new string[] {"truck 2", "truck 3"};

Sortie attendue :
long expected = 13;

Explication

A 2-axle truck pays just the car rate 5; a 3-axle truck pays 5 + 3 = 8. Total 13.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Start from the car rate of 5 and adjust per vehicle type.

Indice 2

A truck pays extra only for axles beyond the first two.

Indice 3

Motorbikes override the base rule with a fixed 2.

Complexité attendue

O(number of vehicles) time, O(1) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.