Get TLE. How to improve it?


  • 0
    S
    public static int minimumTotal(ArrayList<ArrayList<Integer>> triangle){
    	int n = triangle.size();
    	
    	ArrayList<Integer> ret = triangle.get(0);
    	
    	for(int i = 0; i < n - 1; i++){
    		ArrayList<Integer> next_row = triangle.get(i + 1);
    		ArrayList<Integer> temp_row = new ArrayList<Integer>();
    		for(int j = 0; j < ret.size(); j++){
    			int sum1 = ret.get(i) + next_row.get(i);
    			int sum2 = ret.get(i) + next_row.get(i + 1);
    			temp_row.add(sum1);
    			temp_row.add(sum2);
    		}
    		ret = temp_row;
    		
    	}
    	int min = ret.get(0);
    	for(int j = 0; j < ret.size() - 1; j++){
    		if(ret.get(j + 1) < ret.get(j)){
    			min = ret.get(j + 1);
    		}
    	}
    	
    	return min;
    

    }


  • 0
    S

    Hi @Sining, thanks for your post. But you should follow the rules here. At least some words to explain your code.


Log in to reply
 

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