My Short and Neat Java Solution - Using HashMap


  • 0
    F
    public int leastBricks(List<List<Integer>> wall) {
    	Map<Integer, Integer> map = new HashMap<>();
    	for (List<Integer> row : wall) {
    		int sum = 0;
    		for (int i = 0; i < row.size() - 1; i++) {
    			sum += row.get(i);
    			map.put(sum, map.getOrDefault(sum, 0) + 1);
    		}
    	}
    	int max = 0;
    	for (int i : map.keySet()) {
    		max = Math.max(max, map.get(i));
    	}
    	return wall.size() - max;        
    }

Log in to reply
 

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