Largest Permutation Discussions | Algorithms | HackerRank
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.
My Java solution with linear time and space complexity:
publicstaticList<Integer>largestPermutation(intk,List<Integer>arr){if(arr.size()==1)returnarr;//no permutations possible//get the indices of each val in arrMap<Integer,Integer>valMap=newHashMap<>();for(inti=0;i<arr.size();i++)valMap.put(arr.get(i),i);//store the max val as size of arrintmax=arr.size(),j=0;//iterate through the arr and do k permutationswhile(j<arr.size()&&k>0){if(arr.get(j)!=max){inttemp=arr.get(j);arr.set(j,max);arr.set(valMap.get(max),temp);valMap.put(temp,valMap.get(max));k--;}max--;j++;}returnarr;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Permutation
You are viewing a single comment's thread. Return to all comments →
My Java solution with linear time and space complexity: