ง่าย 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก