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.
- Prepare
- Algorithms
- Greedy
- Largest Permutation
- Discussions
Largest Permutation
Largest Permutation
Sort by
recency
|
381 Discussions
|
Please Login in order to post a comment
Here is my c++ solution O(n), you also have a vidéo explanation here : https://youtu.be/baZOM6KLSWM
Ruby
//Java8 // Write your code here int indx=0; Integer tempData; while (k>0) { if(indx>=arr.size()-1){ break; } if(arr.size()!=0 && arr.size()>2){ Integer max = arr.stream().skip(indx).max(Integer::compare).get(); tempData = arr.get(indx); int mxindx = arr.indexOf(max); if(mxindx==indx){ indx++; }else{ arr.set(indx,max); arr.set(mxindx,tempData); indx++; k--; } }else if(arr.size()==1){ break;} else if (arr.size()==2){ Boolean flag = arr.get(0)>arr.get(1)?true:false; if(!flag){ int a = arr.get(0); int b = arr.get(1); arr.set(0, b); arr.set(1, a); k--; } break; } }
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star :) )