Блог пользователя Infinite.Loop

Автор Infinite.Loop, история, 5 лет назад, По-английски

This is my solution when I used unordered_map.

This is my solution when I used ordered_map.

It is seen that unordered_map performs better than ordered_map so how can TLE get resolved with ordered_map but not with unordered_map???

  • Проголосовать: нравится
  • -18
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

The hashing is not perfect and with specific test cases, every find, insert etc. works in O(n) instead of O(1). Refer to this for more detail.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
| map             | unordered_map

Ordering | increasing order | no ordering | (by default) |

Implementation | Self balancing BST | Hash Table | like Red-Black Tree |

search time | log(n) | O(1) -> Average | | O(n) -> Worst Case

Insertion time | log(n) + Rebalance | Same as search

Deletion time | log(n) + Rebalance | Same as search

so the answer of your problem is in worse case unordered_map uses O(n) for each insertion .So your insertion in the testcase no 12 will take upto O(n^2) in your unordered map ,but it will take only O(nlog(n)) in map.