My java solution


  • 0
    C
          import java.util.*;
          public class Solution{
          public int minimumTotal(List<List<Integer>> triangle) {
          if(triangle .size() == 0)
        	return 0;
    	
        if(triangle.size() == 1)
        {
        	int result=0;
        	List<Integer> l = new ArrayList();
        	l=triangle.get(0);
        	
        	for(int i=0;i<l.size();i++)
            {
            	result = result<l.get(i)?result:l.get(i);
            }
            
    		return result;
        }
        
        ArrayList<ArrayList<Integer>> list = new ArrayList();
        //base case
        ArrayList<Integer> l0 = new ArrayList<Integer> ();
        l0.add(triangle.get(0).get(0));
        list.add(l0);
    	
        for(int i=1;i<triangle.size();i++)
        {
        	ArrayList<Integer> tmp = new ArrayList();
        	for(int j=0;j<triangle.get(i).size();j++)        	
        	{
        		if(j == 0)
        		{
        			tmp.add(list.get(i-1).get(0)+triangle.get(i).get(0));      			
        		}
        		
        		else if(j == triangle.get(i).size()-1)
        		{
        			tmp.add(list.get(i-1).get(j-1)+triangle.get(i).get(j));
        		}
        		
        		else
        		{       			
        			int min = getMinValue(list.get(i-1).get(j),list.get(i-1).get(j-1));
        			tmp.add(min+triangle.get(i).get(j));
        		}
        	}
        	list.add(tmp);
        }
        ArrayList<Integer> resList = list.get(triangle.size()-1);
        
        int result = resList.get(0);
        
        for(int i=0;i<resList.size();i++)
        {
        	result = result<resList.get(i)?result:resList.get(i);
        }
    	return result;
    }
    
    public int getMinValue(int a, int b)
    {
    	return a>b?b:a;
    }
    

    }


Log in to reply
 

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