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.
A straight forward approach but i havent used counting sort but a dictionary to save the string as ecountered in the loop with key as the integer associated with it. This was the sorting part is done on the go. Aslo available on github
# Complete the countSort function below.defcountSort(arr,n):coll=dict()#keyisx[i]andvalueislistofalls[i]whichhavex[i]count=0#willbeusedtoreplacefirsthalfstringwith'-'for[i,j]inarr:ifcount<n/2:coll.setdefault(int(i),[]).append('-')#firsthalfs[i]savedas'-'count+=1else:coll.setdefault(int(i),[]).append(j)#nexthalfs[i]asoriginalstringans=[]ele=list(sorted(list(coll.keys())))#getallx[i]andsortforiinele:temp=coll[i]ans.extend(temp)print(' '.join(ans))if__name__=='__main__':n=int(input().strip())arr=[]for_inrange(n):arr.append(input().rstrip().split())countSort(arr,n)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →
Python 3
Time complexity: n
A straight forward approach but i havent used counting sort but a dictionary to save the string as ecountered in the loop with key as the integer associated with it. This was the sorting part is done on the go. Aslo available on github