My intuitive java solution using hashmap


  • 0
    D
    
    public class Solution {
        public int leastBricks(List<List<Integer>> wall) {
            if(wall==null || wall.size()==0){
                return 0;
            }
            int n = wall.size();
            int len = 0;
            for(Integer v : wall.get(0)){
                len += v;
            }
            Map<Integer,Integer> map = new HashMap<>();
            for(List<Integer> line : wall){
                int curLoc = 0;
                for(Integer w : line){
                    curLoc += w;
                    if(curLoc == len){
                       break;
                    }
                    if(map.containsKey(curLoc)){
                        map.put(curLoc, map.get(curLoc)+1 );
                    } else{
                        map.put(curLoc, 1);
                    }
                }
            }
            int max = -1;
            for(Map.Entry<Integer,Integer> entry : map.entrySet()){
                if(entry.getValue() > max){
                    max = entry.getValue();
                }
            }
            if (max == -1){
                max = 0;
            }
            return n-max;
            
        }
    }
    

Log in to reply
 

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