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

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

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

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

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