Pushkar-PM's blog

By Pushkar-PM, history, 6 weeks ago, In English

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? I have added code in comments thank u.

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Pushkar-PM (previous revision, new revision, compare).

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it
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];
        }
»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

I am not getting how to format code in question so putting code in comments even though i used block.

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

why downvotes? Cant we ask doubts on blog. Just asking whether it is not allowed

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Pushkar-PM (previous revision, new revision, compare).