We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionreadLine(){returninputString[currentLine++];}// Complete the minimumSwaps function below.functionminimumSwaps(arr){letcount=0;lettemp;// hashmap solutionconsthashMap=newMap();//o(n)for(leti=0;i<arr.length;i++){hashMap.set(arr[i],i);}//o(n)for(leti=0;i<arr.length;i++){if(arr[i]!==i+1){count++;letb=hashMap.get(i+1);temp=arr[i];arr[i]=arr[b];// indexOf would make this O(n^2) used a hashmap instead. hashMap.set(arr[i],i);arr[b]=temp;hashMap.set(arr[b],b);}}returncount;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
javascript solution with hashmap.
Too tired to understand the other solution.