I think my code's neat, why it runs slowly?(JAVA DP)


  • 0
    S
    public class Solution {
    	public int minimumTotal(List<List<Integer>> triangle) {
    		int hi = triangle.size();
    		int[] sum = new int[hi];
    		
    		for(int i=0; i<hi; i++){
    			List li = triangle.get(i);
    			for(int j=li.size()-1; j>=0; j--){
    				int num = (int)li.get(j);
    				if(j==0)
    					sum[0] = sum[0]+num;
    				else if(j==li.size()-1)
    					sum[j] = sum[j-1] + num;
    				else
    					sum[j] = Math.min(sum[j-1]+num,sum[j]+num);
    			}
    		}
    		Arrays.sort(sum);
    		return sum[0];
        }
    }

Log in to reply
 

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