मध्यम 0 हल किए गए 0% स्वीकृति

Library Checkout System

Run a small library system: add copies, check books out with a three book limit, return them and answer queries.

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

  • Track copies per title with a map
  • Track each member's books
  • Apply checks in the stated order

समस्या

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

A school library tracks how many copies of each title are on the shelf and which titles each member holds. Titles and member names are single words. Process these operations in order:

  • add <title>: one more copy of the title goes on the shelf (no output).
  • checkout <member> <title>: if the member already holds 3 books, output LIMIT. Otherwise, if no copy is on the shelf, output UNAVAILABLE. Otherwise the member takes a copy and the output is OK. A member may hold several copies of the same title.
  • return <member> <title>: if the member holds that title, one copy goes back on the shelf and the output is OK; otherwise output ERROR.
  • available <title>: output the number of copies on the shelf.
  • books <member>: output how many books the member holds.

Implement library_checkout(operations).

Input

operations: The operations, processed in order.

Output

Return every output as a string, in the order they happen (numbers are written in base 10, like "2").

सीमाएँ

0 ≤ number of operations ≤ 2,000

Titles and names have 1 to 20 letters or digits

उदाहरण

उदाहरण 1
इनपुट:
operations = ["add Dune", "checkout ana Dune", "checkout ben Dune", "available Dune", "return ana Dune", "checkout ben Dune"]

अपेक्षित आउटपुट:
expected = ["OK", "UNAVAILABLE", "0", "OK", "OK"]
इनपुट:
std::vector<std::string> operations = {"add Dune", "checkout ana Dune", "checkout ben Dune", "available Dune", "return ana Dune", "checkout ben Dune"};

अपेक्षित आउटपुट:
std::vector<std::string> expected = {"OK", "UNAVAILABLE", "0", "OK", "OK"};
इनपुट:
string[] operations = new string[] {"add Dune", "checkout ana Dune", "checkout ben Dune", "available Dune", "return ana Dune", "checkout ben Dune"};

अपेक्षित आउटपुट:
string[] expected = new string[] {"OK", "UNAVAILABLE", "0", "OK", "OK"};

व्याख्या

Ana takes the only copy, so Ben gets UNAVAILABLE and 0 copies are left. After Ana returns it, Ben gets it: ["OK", "UNAVAILABLE", "0", "OK", "OK"].

उदाहरण 2
इनपुट:
operations = ["add A", "add B", "add C", "add D", "checkout kim A", "checkout kim B", "checkout kim C", "checkout kim D", "return kim D", "books kim"]

अपेक्षित आउटपुट:
expected = ["OK", "OK", "OK", "LIMIT", "ERROR", "3"]
इनपुट:
std::vector<std::string> operations = {"add A", "add B", "add C", "add D", "checkout kim A", "checkout kim B", "checkout kim C", "checkout kim D", "return kim D", "books kim"};

अपेक्षित आउटपुट:
std::vector<std::string> expected = {"OK", "OK", "OK", "LIMIT", "ERROR", "3"};
इनपुट:
string[] operations = new string[] {"add A", "add B", "add C", "add D", "checkout kim A", "checkout kim B", "checkout kim C", "checkout kim D", "return kim D", "books kim"};

अपेक्षित आउटपुट:
string[] expected = new string[] {"OK", "OK", "OK", "LIMIT", "ERROR", "3"};

व्याख्या

Kim reaches 3 books, so the fourth checkout gives LIMIT; returning D fails with ERROR because Kim never got it, and Kim holds 3.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Use one map from title to shelf copies and one from member to the titles they hold.

संकेत 2

Check the 3 book limit before checking the shelf.

संकेत 3

A return only works if that title is in the member's list; remove just one copy of it.

अपेक्षित जटिलता

O(q) time, O(q) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

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

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

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