My java solution


  • 0
    C
      import java.util.*;
      public class Solution{
      public int minimumTotal(List<List<Integer>> triangle) {
    	if(triangle.size() == 0)
    		return 0;
    	
    int [] res = new int[triangle.size()];
    int min = Integer.MAX_VALUE;
    res[0] = triangle.get(0).get(0);
    for(int i=1;i<triangle.size();i++)
    {
    	int tmp[] = new int[i+1];
    	for(int j=0;j<=i;j++)
    	{
    		if(j == 0)
    		{
    			tmp[j]=  triangle.get(i).get(j)+res[j];
    		}
    		else if(j == triangle.get(i).size()-1)
    		{
    			tmp[j] = res[j-1]+triangle.get(i).get(j);
    		}
    		else
    		{
    			tmp[j] = getMinV(res[j]+triangle.get(i).get(j),res[j-1]+triangle.get(i).get(j));
    		}
    	}
    	
    	for(int k = 0;k<=i;k++)
    	{
    		res[k] = tmp[k];
    	}
    	
    }
    for(int i=0;i<triangle.size();i++)
    	{
    	min = min>res[i]?res[i]:min;
    	}
    	
    	return min;
    }
    
    private int getMinV(Integer i, Integer j) {
    	return i>j?j:i;
    }	
    

    }


Log in to reply
 

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