Help in spoj trigraph problem

Revision en1, by pushkar31, 2021-01-22 19:27:20

Can anyone plz tell me why my code gives tle.On google search most of them have solved it using bottom top dp. Here's the lonk for problem.Is there any bug in my code or top bottom is slow due to function calls?here's the main code snippet- Code for (int i = 0; i < t;) { n = scanner.nextInt(); c++; if(n==0){ break; } fib=new int[n][3]; fib1=new int[n][3]; for (int j = 0; j < n; j++) { for (int k = 0; k < 3; k++) { fib[j][k]=scanner.nextInt(); } } long s=fib(0,1); printWriter.println(c+"."+" "+(s+fib[n-1][1])); } printWriter.flush(); } public int fib(int a,int b){ if(a==n-1&&b==1){ return 0; } else if(a<0||b<0){ return 10000000; } else if(a>n-1||b>2){ return 10000000; } else if(fib1[a][b]>0){ return fib1[a][b]; } fib1[a][b]=Math.min(fib[a][b]+fib(a+1,b-1),Math.min(fib[a][b]+fib(a,b+1),Math.min(fib[a][b]+fib(a+1,b),fib[a][b]+fib(a+1,b+1)))); return fib1[a][b]; }

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English pushkar31 2021-01-23 09:43:28 39 Tiny change: 'ion calls?' -> 'ion calls? I have added code in comments thank u.'
en3 English pushkar31 2021-01-22 19:46:28 1191
en2 English pushkar31 2021-01-22 19:34:02 20
en1 English pushkar31 2021-01-22 19:27:20 1454 Initial revision (published)