CAN ANYONE TELL WHAT IS THE PROBLEM HERE...??? WHY THEY ARE NOT CONSIDER "J-1" AS AN ADJACENT..??


  • 0
    D

    class Solution {
    public:
    int minn(int a,int b,int c){
    if(a<b && a<c) return a;
    if(b<a && b<c) return b;
    return c;
    }
    int minimumTotal(vector<vector<int> >& triangle)
    {
    int ts = triangle.size()-1;
    int n = triangle[ts].size();
    int tri[ts+1][n];

     for(int i=0;i<n;i++)
    		for(int j=0;j<n;j++)
    			tri[i][j]=INT_MAX;
    
    tri[0][0]=triangle[0][0];
    
    for(int i=1; i<n; i++)
    {
    	for(int j=0;j<=i;j++)
    	{
    		if(j==0)
    
    			tri[i][j]=triangle[i][j] + min(tri[i-1][j],tri[i-1][j+1]);
    		else if(j==n-1)
    
    			tri[i][j]=triangle[i][j] + min(tri[i-1][j-1],tri[i-1][j]);
    		else
    
    			tri[i][j]=triangle[i][j] 
    					+ minn(tri[i-1][j-1],tri[i-1][j],tri[i-1][j+1]);
    	}
    }
    for(int i=0; i<n; i++)
    {
    	for(int j=0;j<=i;j++)
    		cout<<tri[i][j]<<" ";
    	cout<<endl;
    }
    int m=INT_MAX;
    for(int i=0;i<n;i++)
    {
    	if(m>tri[ts][i])
    	{
    		m=tri[ts][i];
    	}
    }	
    return m;	
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.