Easy understand Java solution using Hashmap


  • 0
    Q
       public int leastBricks(List<List<Integer>> wall) {
    			HashMap <Integer, Integer> table = new HashMap <>();
    			for (int i = 0; i < wall.size(); i++) {
    				List<Integer> rows = wall.get(i);
    				int index = 0;
    				for (int j = 0; j < rows.size()-1; j++) {
    					index += rows.get(j);
    					table.put(index, table.getOrDefault(index, 0)+1);
    				}
    			}
    			Set<Integer> set = table.keySet();
    			int size = wall.size();
    			int min = wall.size();
    			for (Integer i : set) {
    				if(size - table.get(i)<min){
    					min = size - table.get(i);
    				}
    			}
    			return min;
    
    		}
    
    

Log in to reply
 

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