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.
Yes, U can do that that sort of thing, but it would be a messy to firstly count all numbers and then create a discrete array of string buffers and also the space complexity may not be improved very much as x is small(x<=100) and u are also using an another array to store values which are not null.
What I meant is something like this. I am unsure if its better, considering the tradeoff is having to check some if statements just to avoid creating empty Buffers. I am unsure what the "price" for creating empty buffers is.. I am pretty sure this would be better for a large scale with lots of empty Buffers, but this is obviously not the case here.
and i was getting wrong answer all the time.
In the step st[k]=st[k].append(s); i was getting all the array elements changed instead of the one at index k.
can anyone help me with this??????
what you are doing is creating a reference variable and passing it every element ,so the reference to every variable(st[k]) is same.Instead what you have to do is initialise each variable independently so that each of them have has their own reference of a StringBuffer variable and changing one might not affect other.
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →
This solution works better than the other solutions discussed here.
import java.util.Scanner;
public class Solution {
}
quite a elegant solution
should've used "int size=scan.nextInt();" instead
https://www.hackerrank.com/challenges/countingsort4/submissions/code/55469006
If anyone is looking for a solution. this is it.
best solution for this problem
pls explain @insane84
Wouldnt it be better to avoid creating all StringBuffers at the start, by checking if a StringBuffer in the Array is null and creating one if it is?
Yes, U can do that that sort of thing, but it would be a messy to firstly count all numbers and then create a discrete array of string buffers and also the space complexity may not be improved very much as x is small(x<=100) and u are also using an another array to store values which are not null.
What I meant is something like this. I am unsure if its better, considering the tradeoff is having to check some if statements just to avoid creating empty Buffers. I am unsure what the "price" for creating empty buffers is.. I am pretty sure this would be better for a large scale with lots of empty Buffers, but this is obviously not the case here.
I had the same logic as you , although i was using Arrays.fill(st, new StringBuffer) instead of the loop
for(int i=0;i<100;i++) { st[i]=new StringBuffer(); }
and i was getting wrong answer all the time. In the step st[k]=st[k].append(s); i was getting all the array elements changed instead of the one at index k. can anyone help me with this??????
what you are doing is creating a reference variable and passing it every element ,so the reference to every variable(st[k]) is same.Instead what you have to do is initialise each variable independently so that each of them have has their own reference of a StringBuffer variable and changing one might not affect other.
Thanks to your comment I don't have Time problem anymore thanks again.