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.
Gotta say, I'm very disappointed when the time constraints are such that the fastest way to solve something in Javascript continually gets a timeout. If HackerRank presents a problem then they should disable a language if they're going to put unrealistic time constraints on it.
Heres a solution I tried that worked other than timeout:
varrotArr=Object.values(Object.assign({},arr));varminVal;varmaxVal=Math.max(...rotArr);varminValInd;varcurrentVal;varswapCount=0;for(vari=0;i<rotArr.length;i++){// get minimum valuesminVal=Math.min(...arr);if(minVal===rotArr[i]){arr.shift()continue;}minValInd=rotArr.indexOf(minVal);currentVal=rotArr[i];rotArr[i]=rotArr[minValInd];rotArr[minValInd]=currentVal;arr=Object.values(Object.assign({},rotArr.slice(i)));arr.shift();minVal=Math.min(...arr);swapCount=swapCount+1;}returnswapCount;
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 →
Gotta say, I'm very disappointed when the time constraints are such that the fastest way to solve something in Javascript continually gets a timeout. If HackerRank presents a problem then they should disable a language if they're going to put unrealistic time constraints on it.
Heres a solution I tried that worked other than timeout: