← Back to topics
Topic

z-hundred

k
kgstefan88
Please, someone who had had solved this problem, see my code and tell me why do i get "Invalid memory reference (memory limit exceeded)" on every test... I used pointers... Maybe I made mistake there... But it works like clock on my PC..
A
Al3kSaNdaR
Did you consider negative numbers ?
k
kinezizbosne
I need help...

http://z-trening.com/submit.php?submit=7100340193&subm_code=1
d
demjan0001
maybe you forgot to decrease i?

while(strcmp(p[i],p[i-1])<0 && i>0){
strcpy(d,p[i]);
strcpy(p[i],p[i-1]);
strcpy(p[i-1],d);
i--;
}


But you will still get TLE, because time complexity of your solution is O( N * N * length of number), so in worst case that is 2000 * 2000 * 1000 operations, and that is just too many.

But if you would use quick sort, or some other NlogN complexity sort, you would have time complexity O( N*logN*length of number), which is in worst case 2000 * 20 * 1000, which is fine...
d
demjan0001
Also don't print all numbers:

//system("pause");
for(i=0;i<cnt;i++){
printf("%s\n",p[i]);
}