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.
funminimumBribes(q:Array<Int>):Unit{varresult=0varlist=q.toMutableList()for(indexin(q.lastIndex)downTo0){valsortedValue=(index+1)if(list[index]==sortedValue){// make the list smaller so that next iteration would take less time to find the lastIndexOflist.removeAt(index)continue}// search from tail of list gets the index quicker in this casevalunSortedIndex=list.lastIndexOf(sortedValue)valshift=(index-unSortedIndex)if(shift>2){println("Too chaotic")return}list.removeAt(unSortedIndex)result+=shift}println(result)}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
My Kotlin solution