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.
defbiggerIsGreater(w):# find longest non increasing suffixnis=[[-1,w[-1]]]i=0whilei<len(w):try:ifw[-2-i]>=w[-1-i]:nis.append([-2-i,w[-2-i]])i+=1else:# find pivotpivot=[-2-i,w[-2-i]]breakexceptIndexError:return"no answer"# find rightmost successor of pivot in the suffixnis=nis[::-1]rightmost=[]forkey,valueinnis:ifvalue>pivot[1]:rightmost.append([key,value])rightmost.sort(key=lambdax:x[1])successor=rightmost[0]successor_list=[]forkey,valueinrightmost:ifvalue==successor[1]:successor_list.append([key,value])successor=successor_list[-1]# swap the pivot and rightmost successornis_str=""forkey,valueinnis:nis_str+=valuenis_list=list(nis_str)nis_list[successor[0]]=pivot[1]nis_str="".join(nis_list)prefix=w[:-len(nis)-1]+successor[1]# reverse the suffixsuffix=nis_str[::-1]returnprefix+suffix
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
python