Input: A = { 3, 2, 1 }, B = { 1, 3, 2 }
Output: 7
Explanation:
For A = 3 and B = 1 => Since the front of queue A does not match with the front of queue B. Pop and Push 3 at the back of Queue. Then, A becomes { 2, 1, 3 } and time consumed = 2 ( 1 unit of time for each push and pop operation)
For A = 2 and B = 1 => Since the front of queue A does not match with the front of queue B. Pop and Push 2 at the back of Queue. Then, A becomes { 1, 3, 2 } and time = 2 + 2 = 4
For A = 1 and B = 1 => Since the front of queue, A equals to front of queue B. Pop 1 from both the queue and execute it, Then A becomes { 3, 2 } and B becomes { 3, 2 } and time = 4 + 1 = 5
For A = 3 and B = 3 => Since the front of queue, A equals to front of queue B. Pop 3 from both the queue and execute it, Then A becomes { 2 } and B becomes { 2 } and time = 5 + 1 = 6
For A = 2 and B = 2 => Since the front of the queue, A equals to front of queue B. Pop 2 from both the queue and execute it. All the tasks are executed. time = 6 + 1 = 7
Therefore the total time is 7.
Input: A = { 3, 2, 1, 4 }, B = { 4, 1, 3, 2 }
Output: 14