Problema
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, outputLIMIT. Otherwise, if no copy is on the shelf, outputUNAVAILABLE. Otherwise the member takes a copy and the output isOK. 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 isOK; otherwise outputERROR.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").
Restricciones
0 ≤ number of operations ≤ 2,000
Titles and names have 1 to 20 letters or digits
Ejemplos
Entrada:
operations = ["add Dune", "checkout ana Dune", "checkout ben Dune", "available Dune", "return ana Dune", "checkout ben Dune"]
Salida esperada:
expected = ["OK", "UNAVAILABLE", "0", "OK", "OK"]
Entrada:
std::vector<std::string> operations = {"add Dune", "checkout ana Dune", "checkout ben Dune", "available Dune", "return ana Dune", "checkout ben Dune"};
Salida esperada:
std::vector<std::string> expected = {"OK", "UNAVAILABLE", "0", "OK", "OK"};
Entrada:
string[] operations = new string[] {"add Dune", "checkout ana Dune", "checkout ben Dune", "available Dune", "return ana Dune", "checkout ben Dune"};
Salida esperada:
string[] expected = new string[] {"OK", "UNAVAILABLE", "0", "OK", "OK"};
Explicación
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"].
Entrada:
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"]
Salida esperada:
expected = ["OK", "OK", "OK", "LIMIT", "ERROR", "3"]
Entrada:
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"};
Salida esperada:
std::vector<std::string> expected = {"OK", "OK", "OK", "LIMIT", "ERROR", "3"};
Entrada:
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"};
Salida esperada:
string[] expected = new string[] {"OK", "OK", "OK", "LIMIT", "ERROR", "3"};
Explicación
Kim reaches 3 books, so the fourth checkout gives LIMIT; returning D fails with ERROR because Kim never got it, and Kim holds 3.
Cómo abordarlo
Inténtalo primero por tu cuenta y luego abre las pistas una a una.
Pista 1
Use one map from title to shelf copies and one from member to the titles they hold.
Pista 2
Check the 3 book limit before checking the shelf.
Pista 3
A return only works if that title is in the member's list; remove just one copy of it.
Complejidad esperada
O(q) time, O(q) extra space
Referencias
Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.