簡単 0件解決済み 正答率0%

Vehicle Tolls

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

0 ≤ number of vehicles ≤ 100,000

2 ≤ axles ≤ 10

例

例 1
入力:
vehicles = ["car", "motorbike", "truck 4"]

期待される出力:
expected = 18
入力:
std::vector<std::string> vehicles = {"car", "motorbike", "truck 4"};

期待される出力:
long long expected = 18;
入力:
string[] vehicles = new string[] {"car", "motorbike", "truck 4"};

期待される出力:
long expected = 18;

解説

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

例 2
入力:
vehicles = ["truck 2", "truck 3"]

期待される出力:
expected = 13
入力:
std::vector<std::string> vehicles = {"truck 2", "truck 3"};

期待される出力:
long long expected = 13;
入力:
string[] vehicles = new string[] {"truck 2", "truck 3"};

期待される出力:
long expected = 13;

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

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

ヒント 2

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

ヒント 3

Motorbikes override the base rule with a fixed 2.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。