Java HashMap Solution


  • 0

    Code is modified. I found it is not necessary to get the width of the wall, just skip the last brick on each row.

    public int leastBricks(List<List<Integer>> wall) {
        Map<Integer, Integer> memo = new HashMap<>();
        int maxCount = 0;
        for(int i = 0; i<wall.size(); i++){
            int len = 0;
            for(int j = 0; j<wall.get(i).size()-1; j++){
                len += wall.get(i).get(j);
                if(memo.containsKey(len)){
                    memo.put(len,memo.get(len)+1);
                }
                else{
                    memo.put(len, 1);
                }
                maxCount = Math.max(memo.get(len),maxCount);
            }
        }
        return wall.size()-maxCount;
    }

Log in to reply
 

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