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

Автор wish_me, история, 9 лет назад, По-английски

There are n-pairs and therefore 2n people. everyone has one unique number ranging from 1 to 2n. All these 2n persons are arranged in random fashion in an Array of size 2n. We are also given who is partner of whom. Find the minimum number of swaps required to arrange these pairs such that all pairs become adjacent to each other.

n<10^3 arr.size<10^3

Input: n = 3

pairs[] = {1->3, 2->6, 4->5} // 1 is partner of 3 and so on

arr[] = {3, 5, 6, 4, 1, 2}

Output: 2

We can get {3, 1, 5, 4, 6, 2} by swapping 5 & 6, and 6 & 1

(Google Interview)

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

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится
»
9 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +7 Проголосовать: не нравится

Up. I met this problem before and know linear reference solution but I can't prove it :(