Straight Java - HashMap


  • 0

    Note: Collections.max(map.values()); can easily find the max value in hashmap, which represents the position that has the most overlap seams between bricks.

        public int leastBricks(List<List<Integer>> wall) {
            Map<Integer, Integer> map = new HashMap<>(); 
            for (int i = 0; i < wall.size(); i++) {
                int pos = 0; 
                for (int j = 0; j < wall.get(i).size() - 1; j++) {
                    pos += wall.get(i).get(j); 
                    map.put(pos, map.getOrDefault(pos, 0) + 1); 
                }
            }
            return map.isEmpty() ? wall.size() : wall.size() - Collections.max(map.values());
        }

Log in to reply
 

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