JAVA HASHMAP SOLUTION


  • 0
    B
    public class Solution {
        public int leastBricks(List<List<Integer>> wall) {
            if(wall == null || wall.size() == 0){
                return 0;
            }
            HashMap<Integer,Integer> sumCount = new HashMap<Integer,Integer>();
            int size = wall.size();
            int maxSame = 0;
            for(List<Integer> row : wall){
                int sum = 0;
                for(int i = 0 ; i < row.size()-1;i++){
                    sum += row.get(i);
                    int num = 1;
                    if(sumCount.containsKey(sum)){
                        num = sumCount.get(sum) + 1; 
                    }
                    sumCount.put(sum,num);
                    maxSame = Math.max(maxSame,num);
                }
            }
            return size - maxSame;
        }
    }

Log in to reply
 

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