문제
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, ormissingif 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
예시
입력:
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"].
입력:
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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.