中級 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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。