Concise Java Solution using HashMap storing prefixSums


  • 0
    public int leastBricks(List<List<Integer>> wall) {
        Map<Integer,Integer> map = new HashMap<>();
        for(List<Integer> row : wall) {
            int prefixSum = 0;
            for(int i=0; i<row.size()-1 /* ignore last */; i++) {
                prefixSum += row.get(i);
                map.put(prefixSum, map.getOrDefault(prefixSum, 0) + 1);
            }
        }
        int maxSame = 0;
        for(Map.Entry<Integer,Integer> entry : map.entrySet())
            maxSame = Math.max(maxSame, entry.getValue());
        return wall.size() - maxSame;
    }

Log in to reply
 

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