โจทย์
Polymorphism lets code call the same method on different objects and get behaviour specific to each one. Here every channel computes the cost of a message its own way.
Each message is <channel> <text>, where the text is everything after the first space:
email: costs 1.sms: costs 5 for every started block of 160 characters of text (1 to 160 characters is one block).push: free.
Implement notification_cost(messages).
Input
messages: The messages to send. SMS texts have at least one character.
Output
Return the total cost.
เงื่อนไข
0 ≤ number of messages ≤ 10,000
Texts have 1 to 2,000 characters
ตัวอย่าง
อินพุต:
messages = ["email Welcome!", "sms Your code is 1234", "push New follower"]
ผลลัพธ์ที่คาดหวัง:
expected = 6
อินพุต:
std::vector<std::string> messages = {"email Welcome!", "sms Your code is 1234", "push New follower"};
ผลลัพธ์ที่คาดหวัง:
long long expected = 6;
อินพุต:
string[] messages = new string[] {"email Welcome!", "sms Your code is 1234", "push New follower"};
ผลลัพธ์ที่คาดหวัง:
long expected = 6;
คำอธิบาย
Email costs 1, the short SMS uses one block (5) and push is free: 6.
อินพุต:
messages = ["sms aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
ผลลัพธ์ที่คาดหวัง:
expected = 10
อินพุต:
std::vector<std::string> messages = {"sms aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
ผลลัพธ์ที่คาดหวัง:
long long expected = 10;
อินพุต:
string[] messages = new string[] {"sms aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
ผลลัพธ์ที่คาดหวัง:
long expected = 10;
คำอธิบาย
161 characters need two 160-character blocks: 2 × 5 = 10.
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
Split each message once at the first space into channel and text.
คำใบ้ 2
For SMS, count blocks with ceiling division: (length + 159) / 160.
คำใบ้ 3
Push messages cost nothing, so they add 0.
ความซับซ้อนที่คาดหวัง
O(total length) time, O(1) extra space
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน