आसान 0 हल किए गए 0% स्वीकृति

Notification Costs

Send messages through email, SMS and push channels that each compute their own cost.

आप क्या सीखेंगे

  • Give each channel its own cost rule
  • Round up to whole blocks of characters
  • Split a message into channel and text

समस्या

अनुवाद दिखाएँ

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

उदाहरण

उदाहरण 1
इनपुट:
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.

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

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।