모든 챌린지
보통 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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.