समस्या
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
उदाहरण
इनपुट:
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.
इनपुट:
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।