ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

One Shared Configuration

Simulate a singleton settings object: every part of the program reads and writes the same instance.

สิ่งที่คุณจะได้เรียนรู้

  • Share one object across the whole program
  • Create an instance lazily on first use
  • Read and update settings through one place

โจทย์

ดูฉบับแปล

The singleton pattern makes sure a class has exactly one instance that everyone shares, a common choice for application settings.

Simulate one shared configuration that starts empty. Process the operations in order:

  • set <key> <value>: store the value for the key (replacing any old value).
  • get <key>: read the key's current value, or missing if it was never set.

Because every caller uses the same instance, a value set by one operation is visible to every later get.

Implement shared_configuration(operations).

Input

operations: Strings in the two forms above. Keys and values contain no spaces.

Output

Return the results of the get operations, in order.

เงื่อนไข

1 ≤ number of operations ≤ 10,000

Keys and values have 1 to 20 characters

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
operations = ["set theme dark", "get theme", "get language"]

ผลลัพธ์ที่คาดหวัง:
expected = ["dark", "missing"]
อินพุต:
std::vector<std::string> operations = {"set theme dark", "get theme", "get language"};

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"dark", "missing"};
อินพุต:
string[] operations = new string[] {"set theme dark", "get theme", "get language"};

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"dark", "missing"};

คำอธิบาย

theme was set to dark; language was never set, so it is missing: ["dark", "missing"].

ตัวอย่างที่ 2
อินพุต:
operations = ["set lang en", "set lang vi", "get lang"]

ผลลัพธ์ที่คาดหวัง:
expected = ["vi"]
อินพุต:
std::vector<std::string> operations = {"set lang en", "set lang vi", "get lang"};

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"vi"};
อินพุต:
string[] operations = new string[] {"set lang en", "set lang vi", "get lang"};

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"vi"};

คำอธิบาย

The second set replaces the first value in the same shared instance: ["vi"].

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

All operations read and write the same single object.

คำใบ้ 2

A dictionary from key to value is enough to hold the settings.

คำใบ้ 3

A key that was never set must return the text missing.

ความซับซ้อนที่คาดหวัง

O(number of operations) time, O(number of keys) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

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

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

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

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

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