I was solving bigSorting() problem from hackerrank:
Consider an array of numeric strings where each string is a positive number with anywhere from to digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array.
I know it works as follows:
def bigSorting(unsorted):
return sorted(unsorted, key=int)
But I didnt guess this approach earlier. Initially I tried below:
def bigSorting(unsorted):
int_unsorted = [int(i) for i in unsorted]
int_sorted = sorted(int_unsorted)
return [str(i) for i in int_sorted]
However, for some of the test cases, it was showing time limit exceeded. Why is it so?
PS: I dont know exactly what those test cases were as hacker rank does not reveal all test cases.
source https://stackoverflow.com/questions/73007397/sorting-large-arrays-of-big-numeric-stings
Comments
Post a Comment