Java solution


  • 0
    C
    public int leastBricks(List<List<Integer>> wall) {
            // map to index the brick break in a row 
            Map<Integer, Integer> map = new HashMap<>();
            
            // number of rows where brick is not crossed
            int maxCount = 0;
            
            for(List<Integer> row : wall){
                int len = 0;
                // -1 because the end one brick needs to be ignored.
                for(int i=0; i<row.size() - 1; i++){
                    len += row.get(i);
                    map.put(len, map.getOrDefault(len, 0) + 1);
                    maxCount = Math.max(maxCount, map.get(len));
                }
            }
            
            // number of rows where brick is crossed
            return wall.size() - maxCount;
        }
    

Log in to reply
 

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