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.
@marioquiroga2912 Just brilliant !! But you are not doing what it says to do.
You are just appending the strings in the order they appear at their poisitions(which is a great idea though), here we have been told to implement counting sort.
So I implemented it, but my 5th test case is failing(Seg fault), which i think is due to memory limits.
i have used 3 arrays of size n(1<=n<=1000000), help please!
Here's my submission link mySubmission
define n as long long int
make your indexes and pos arrays in vector
like vector indexes(n); vector pos(n);
rest of the things remain same.
Brilliant approach using count sort though.
@marioquiroga2912 Just one correction, since all x(keys) are between 1 to 100. You don't need to allocate string ar[] of size 'n', 101 is enough and hence the limit of last for loop would also change.
For anyone stuck at testcase #5: using same above solution, but need some revise:
1. ostringstream instead of string (ostringstream ar[100];)
2. 100 instead of n when alocate ar because 0 <= x < 100.
3. using ostringstream to build output string and print it to stdout only one times when finished.
marioquiroga2912: thank so much for your brilian solution.
Good luck you guys.
In C++, String need contiguous memory to store sequence of character. When you need to expand string by insert or append, If current capacity of string is not enough, it need to allocation memmory with proper capacity, and copy old string to this position before expand this string. So it need many time to do all this action and make time out in this challenges because you append string many times. ostringstream don't need contiguous memory. It store list of string object. In addition, If you know exactly maximum size of string, you can using reserve function to avoid it allocate memory many times.
Your solution is brilliant. I used pair in C++ to establish counting sort table with O(n). Passed all test cases too
voidcountSort(vector<vector<string>>arr){intn=arr.size();// create a "helper" array of vectors of pairs for counting sort with structure:// -----------------------// associated_int vector of pairs// 0 <i, str_ith>, ... // 1 ...// ...// 99 ...// -----------------------// since the integer associated with string in range [0,100) so inititialize size // of "helper" array to 100vector<vector<pair<int,string>>>countingList(100);for(inti=0;i<n;i++){pair<int,string>p(i,arr[i][1]);countingList[stoi(arr[i][0])].push_back(p);}for(inti=0;i<countingList.size();i++){if(countingList[i].size()){for(autoconst&e:countingList[i]){if(e.first<n/2){cout<<"- ";}else{cout<<e.second<<" ";}}}}}
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)
Hey, you have a written an awesome code. Nice and simple approach. But instead if taking string array arr of size n, just take it of size 100. Bcuz the integer x in the constraints range from 0<=x<100 only. Even if n is 30000, ur array will be maximum of 100 elements. Just a thought. But a wonderful approach.
this solution will not work for
4
2 gh
2 rt
1 yh
4 fg
hackerrank has not included these type of test cases where value of integer associated with string has same value as n like in the test case mentioned by me.This solution is wrong according to problem's statement.
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →
I did a simple, efficent and yet understandable solution in C++. I hope it's useful for you guys
@singhabhinandan see this code..try to learn how this works...this code is awesome . thanks @marioquiroga2912 for this brilliant piece of code.
Thank you @WeirdCoder
Here's my python solution which uses counting sort.
Hackerrank - The Full Counting Sort Solution
Thanks bro i read your post and it help me alot to understand your code
My python code
here is problem solution in java python c++ c and javascript programming. https://programs.programmingoneonone.com/2021/05/hackerrank-the-full-counting-sort-solution.html
@marioquiroga2912 this is unconventional but still superior property of Strings.
@marioquiroga2912 Just brilliant !! But you are not doing what it says to do. You are just appending the strings in the order they appear at their poisitions(which is a great idea though), here we have been told to implement counting sort. So I implemented it, but my 5th test case is failing(Seg fault), which i think is due to memory limits. i have used 3 arrays of size n(1<=n<=1000000), help please! Here's my submission link mySubmission
define n as long long int make your indexes and pos arrays in vector like vector indexes(n); vector pos(n); rest of the things remain same. Brilliant approach using count sort though.
@marioquiroga2912 Just one correction, since all x(keys) are between 1 to 100. You don't need to allocate string ar[] of size 'n', 101 is enough and hence the limit of last for loop would also change.
Thank you for great solution. I think string ar[n]; - array size of 100 is enough.
brilliant
nic answer
You are truly an amazing guy !
there is still segmentation fault in test 5
really brilliant
really birilliant
Superb ! Everyone who has commented here (including me) has been like :
Reaaaaallly :)
legend
this won't work for test case 5.
Seriously, This is a simple and yet elegant solution.
brilliant piece of code.
Hats Off!!! Awesome Code!
For anyone stuck at testcase #5: using same above solution, but need some revise: 1. ostringstream instead of string (ostringstream ar[100];) 2. 100 instead of n when alocate ar because 0 <= x < 100. 3. using ostringstream to build output string and print it to stdout only one times when finished. marioquiroga2912: thank so much for your brilian solution. Good luck you guys.
Why did you choose ostringstream over string? Pls let me know. Thks in advance.
In C++, String need contiguous memory to store sequence of character. When you need to expand string by insert or append, If current capacity of string is not enough, it need to allocation memmory with proper capacity, and copy old string to this position before expand this string. So it need many time to do all this action and make time out in this challenges because you append string many times. ostringstream don't need contiguous memory. It store list of string object. In addition, If you know exactly maximum size of string, you can using reserve function to avoid it allocate memory many times.
but where is the sorting??
ar[x] = ar[x] + "-" + " "; ar[x] = ar[x] + s + " "; String will be tatble sorting when it was inputted by above instruction.
Your solution is brilliant. I used pair in C++ to establish counting sort table with O(n). Passed all test cases too
Respect, man. Impressive.
respect man!!
thanks it worked
Nice Code
OKay My mind was blown!!!!!!!
I used multimaps..
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
man you're cool!
'''
'''arr=[] ans={} n=int(input()) for _ in range(n): x,s=input().strip().split() arr.append( [int(x),s] )
for i in range(n//2): arr[i][1]="-"
for k,v in arr: if k in ans: ans[k].append(v) else: ans[k]=[v]
sorted_items=sorted(ans.items(), key=lambda tup:tup[0])
for k , v in sorted_items: print(" ".join( i for i in v),end=" ")
Hey, you have a written an awesome code. Nice and simple approach. But instead if taking string array arr of size n, just take it of size 100. Bcuz the integer x in the constraints range from 0<=x<100 only. Even if n is 30000, ur array will be maximum of 100 elements. Just a thought. But a wonderful approach.
don't show answer in the discussion section , it would be decreased the glory of the question
Why do we need long int if it's given that n < 1000000?
this solution will not work for 4 2 gh 2 rt 1 yh 4 fg hackerrank has not included these type of test cases where value of integer associated with string has same value as n like in the test case mentioned by me.This solution is wrong according to problem's statement.
Nice, i used sorting which added O(nlogn), it can be donw with counting sort easily
This logic is AWESOME! (BY marioquiroga2912)
Looks like I thought the same logic as you but in python : )
This logic is exactly what they want from us