Блог пользователя zenwraight

Автор zenwraight, история, 13 месяцев назад, По-английски

Hi Folks, I came across a piece of code and I am having a hard time figuring out the time complexity of the code. Here is the code:-

This is just for example purpose, basically the q has N values, just for example purpose, I am considering N=3.

map<string, queue<int>> m;
queue<int> q;
q.push(0);
q.push(1);
q.push(2);
m["key"] = q;
queue<int> temp = m["key"];

I want to know will the time complexity of this piece of code be O(1) or O(N)?

My understanding says that it's O(N) because of line queue<int> temp = m["key"] , as we copy the queue to a new variable.

Any pointers or help is greatly appreciated.

Thanks Folks :)

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

»
13 месяцев назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

O(1) because there is no any N at all

  • »
    »
    13 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    [user:oversolver]Thanks a lot for your response Sir, so if the q had size N and m["key"] = q.

    Then would this line queue<int> temp = m["key"] take O(N) time? Basically I want to understand that if I try to store the queue value to an auxiliary queue temp, does it perform this operation in O(1) or O(size of the queue)?

    • »
      »
      »
      13 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Probably yes, copying a $$$N$$$ sized queue (or even vectors, strings, sets, etc.) takes $$$O(size)$$$ time.

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

If the queue size is N and the map size is M then firstly look up through the map will take log(M) time + O(N) time to copying all the values to queue.