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.
This is an optimization on several high-rated Python solutions, which used three counters, even though only two are necessary. I believe that the way I named my counters is also somewhat more intuitive given what they do, so hopefully my solution is easier to understand.
defreverseShuffleMerge(s):fromcollectionsimportCounterrem,out=Counter(s),''reqd={k:v// 2 for k, v in rem.items()}forcurrinreversed(s):#iteratebackwardsifreqd[curr]:#ifcurrisstillreqdwhileoutand(prev:=out[-1])>currandrem[prev]>reqd[prev]:reqd[prev]+=1#ifprevisbiggerandmoreremainout=out[:-1]#thanreqd,putprevbackinreqdreqd[curr]-=1#takecurroutofreqdout+=currrem[curr]-=1#takecurroutofremainreturnout
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Reverse Shuffle Merge
You are viewing a single comment's thread. Return to all comments →
This is an optimization on several high-rated Python solutions, which used three counters, even though only two are necessary. I believe that the way I named my counters is also somewhat more intuitive given what they do, so hopefully my solution is easier to understand.