0
How to solve time complexity and memory issue?
In solving some problems my code is taking more time than given target time. I am unable to solve that issue. Even though code is correct ,there are some changes required to optimise it. But I don't know how to do that. At the end it is incomplete program only. So can any help me what to do for these kind of problems. Give your suggestion to grow further in this programming world. Thank you
6 Respostas
+ 2
Do you use uncounted loop then return to infinite loop?
You need to break; them
+ 1
is your code correct ?
because it works only with length=1 and goes to infinity loop
for(int i=n;i<=1;i--) { // is correct i>=1 ?
for(int j=1;i>=n;j++) { // is correct j>=n instead i>=n (J instead I )?
n=1
i=1 n<=1
j=1 i>=n j++
j=2 i>=n j++
... infinity
0
better if we can see the code
0
ans =0;
int n=a.length;
for(int i=n;i>=1;i--){
int temp=0;
for(int j=1;i>=n;j++){
for(int k=n;k>=1;k--){
if(k>=i&&i>=j&&a[k-1]<a[i-1])
temp++;
}
}
ans+=temp/(i);
}
0
Sorry. Mistake.. in hurry i wrote like that.. can you please check this once
0
If I correct it with way above, I can create array a[1300] and run it under SL limit 10 sec
int[] a=new int[1300];
int n=a.length-1;
for (int i=0; i<=n; i++)
a[i]=i;