Find how many combinations(ways), and use integer's binary string to store going left or going right, but it failed


  • 0
    D
    import java.util.*;
    public class Solution {
        public int minimumTotal(List<List<Integer>> triangle) {
        	int size = triangle.size();
        	int ways = (int)Math.pow(2, size-1);
        	int globalMin = Integer.MAX_VALUE;
        	int counter = ways-1;
        	int bitLength =size-1;
        	for(int i = counter;i>=0;i--){
        		int tempSum=triangle.get(0).get(0);
        		for(int j = 0;j<=bitLength-1;j++){
        			if((counter&(1<<j))==1){
        				tempSum += triangle.get(j+1).get(j+1);
        			}else{
        				tempSum += triangle.get(j+1).get(j);
        			}
        		}
        		
        		if(tempSum<globalMin){
        			globalMin = tempSum;
        		}
        		counter--;
        		
        	}
        	return globalMin;
            
        }
        
      
    }

Log in to reply
 

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