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

다른 사람의 풀이 보기

커뮤니티 솔루션

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