Skip to main content
1 of 4
Amila Senadheera
  • 13.4k
  • 16
  • 31
  • 48

This solution is correct. But it does more than one log(N) time operations on the unordered_map (which uses Red-Balck-Tree implementation in the standard library) and this is a sub-optimal solution:

  void assign(K const& keyBegin, K const& keyEnd, V const& val) {
    if (!(keyBegin < keyEnd))
      return;
    V old_end_value = (*this)[keyEnd];
    m_map.erase(m_map.lower_bound(keyBegin), m_map.upper_bound(keyEnd));
    if (!((*this)[keyBegin] == val)) {
      m_map.insert({keyBegin, val});
    }
    if (!((*this)[keyEnd] == old_end_value)) {
      m_map.insert({keyEnd, old_end_value});
    }
  }
Amila Senadheera
  • 13.4k
  • 16
  • 31
  • 48