O(1)μΌλ‘ mappingμ ꡬμ±ν¨κ³Ό λμμ random accessλ₯Ό μνν μ μλ λ°©λ²μ λν΄μ 묻λ λ¬Έμ μ΄λ€.- λ κ°μ§ μλ£κ΅¬μ‘°λ₯Ό μ‘°ν©νμ¬ ν΄κ²°νλ λ¬Έμ μ΄λ€.
- μ²μμλ μ§μ Nodeλ₯Ό ꡬνν΄μ hash mapκ³Ό linked listλ₯Ό νμ©ν΄μ νμμλ€.
- νμ§λ§
getRandom ꡬνμμ linked listλ₯Ό random access ν μκ° μμ΄μ λ¬Έμ κ° λμλ€.
- μκ°ν΄λ³΄λ insert/deleteλ₯Ό
O(1)μ νλ €λ©΄ unordered_map μ μ°λ©΄ λλ κ±°μλ€. - λ¬Έμ λ random accessλ κ°μ΄ μ 곡ν΄μΌ λλ―λ‘ μ€μ κ°μ
vectorμ μμΉν΄μΌ νλ€λ λ¬Έμ κ° μμλ€. - κ²°κ΅ λ€λ₯Έ μ¬λμ μ루μ
μ μ°Έκ³ νλλ°
unordered_mapμλ vector μμ μμΉλ₯Ό μ μ₯νκ³ vectorμλ μ€μ κ°μ μ μ₯νλ λ°©μμΌλ‘ ν΄κ²°νλ€. - μμ ν λλ (1) μμ νλ κ°κ³Ό
vectorμ λ§μ§λ§ κ°μ swapνκ³ (2)vectorμμ λ§μ§λ§ νλͺ©μ μ κ±° vector::pop_back() (3) unordered_mapμμ swapλ κ°μ μμΉλ₯Ό updateνλ λ°©μμ μ·¨νλ€.
class RandomizedSet {
public:
RandomizedSet()
: positions(),
values() {
}
bool insert(int val) {
auto itr = positions.find(val);
if (itr != positions.end()) return false;
int pos = values.size();
values.push_back(val);
positions[val] = pos;
return true;
}
bool remove(int val) {
auto itr = positions.find(val);
if (itr == positions.end()) return false;
int pos = itr->second;
int lastVal = values[values.size() - 1];
// swap last element to removed position
values[pos] = lastVal;
positions[lastVal] = pos;
// remove element & pos
values.pop_back();
positions.erase(itr);
return true;
}
int getRandom() {
int index = rand() % values.size();
return values[index];
}
private:
unordered_map<int, int> positions;
vector<int> values;
};