Fácil 0 resueltos 0% de aceptación

Vehicle Tolls

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

Lo que aprenderás

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

Problema

Ver traducción

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.

Restricciones

0 ≤ number of vehicles ≤ 100,000

2 ≤ axles ≤ 10

Ejemplos

Ejemplo 1
Entrada:
vehicles = ["car", "motorbike", "truck 4"]

Salida esperada:
expected = 18
Entrada:
std::vector<std::string> vehicles = {"car", "motorbike", "truck 4"};

Salida esperada:
long long expected = 18;
Entrada:
string[] vehicles = new string[] {"car", "motorbike", "truck 4"};

Salida esperada:
long expected = 18;

Explicación

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

Ejemplo 2
Entrada:
vehicles = ["truck 2", "truck 3"]

Salida esperada:
expected = 13
Entrada:
std::vector<std::string> vehicles = {"truck 2", "truck 3"};

Salida esperada:
long long expected = 13;
Entrada:
string[] vehicles = new string[] {"truck 2", "truck 3"};

Salida esperada:
long expected = 13;

Explicación

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

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

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

Pista 2

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

Pista 3

Motorbikes override the base rule with a fixed 2.

Complejidad esperada

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

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.