Very concise Java solution


  • 0
    T
    public class Solution {
        public int leastBricks(List<List<Integer>> wall) {
            if (wall == null) {
                return -1;
            }
            int res = 0;
            Map<Integer, Integer> map = new HashMap<>();
            for (List<Integer> list: wall) {
                int sumSoFar = 0;
                for (int i = 0; i < list.size() - 1; i++) {
                    sumSoFar += list.get(i);
                    if (map.containsKey(sumSoFar)) {
                        map.put(sumSoFar, map.get(sumSoFar) + 1);
                    } else {
                        map.put(sumSoFar, 1);
                    }
                }
            }
            for (Integer i: map.values()) {
            	res = Math.max(res, i);
            }
    
            return wall.size() - res;
        }
    }
    

Log in to reply
 

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