what is the problem in my code ?

Hi I am trying to solve SUMTRIAN problem it is work good in my computer but when i put the code in the website I have the time limit problem
this is my code
if any one can help me ,plz?

  • I am sorry about my English Language

    package codechef;
    import java.util.Scanner;
    class SUMTRIAN {
    private static int Trian[][];
    //private static int SumTrian[];
    private static int rows;
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int case1 = input.nextInt();
    for (int s = 0;s<case1;s++){
    rows= input.nextInt();
    Trian = new int[rowsrows][rowsrows];
    //SumTrian = new int[rows];
    for(int row = 0;row <rows;row++)
    for (int col = 0;col<=row;col++)
    Trian[row][col]=input.nextInt();
    System.out.println(solve(0,0));

      }
    

    }
    public static int solve(int row,int col){
    if(row>rows-1)
    return 0;
    else{
    int t1 = solve(row+1,col);
    int t2 = solve(row+1,col+1);
    int t = Trian[row][col]+max(t1,t2);
    return t;
    }
    }
    public static int max(int t1,int t2){
    int max = t1;
    if(t2>max)
    max = t2;
    return max;

    }
    }

1 Like

Your algorithm is doing an exhaustive search and runs in exponential time, that’s why it’s getting TLE. Try doing a dynamic programming approach :slight_smile: Good luck! Let me know if you want me to explain how to use DP approach in this problem.

1 Like

I found an non-official editorial here: discuss.codechef.com/questions/4557/need-guidance-in-sums-in-triangle-problem#4561

I learned it from tutorial in the code chef by using recursion approach

I will try to solve it by DB approach

Okay brother, good luck :slight_smile: @omar61

This might help-

Also, besides DP, you should take care to never name any package while submitting it on any online judge.

Economics Assignment Help discusses the subject in much detail facilitation on many topics relating to economics, for instance invisible hand; Malthus thoughts about population growth; scarcity, choice, and self-interest within economics and other topics like demand side economics, socio-economics, macroeconomics, microeconomics, aggregate supply, aggregate demand and much more.

Check out best inversion table review on fitensity.net

#include
using namespace std;
long a[100][100];
long sum[100];
int temp,temp1,k;
int main()
{
int m,i,j,n,N;
temp=0;
temp1=1;
k=0;
cin>>n;
while(n>0)
{
cin>>N;
for(i=0;i<N;i++)
for(j=0;j<=i;j++)
{
cin>>a[i][j];
}
for(i=1;i<=N;i++)
{ j=0;
temp=a[i][j];
for(j=0;j<=i;j++)
{
if(temp<=a[i][j])
{
temp=a[i][j];
}
}
temp1=temp1+temp;
if(i==(N))
{
sum[k]=temp1;
if(k==0)
{ --sum[k];
}
k++;
temp1=0;
}
}
n–;
}
for(i=0;i<k;i++)
{
cout<<sum[i]<<"\n";
}
return 0;
}
What’s Wrong with my code?