LeetCode 554: Brick Wall

Posted keepshuatishuati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 554: Brick Wall相关的知识,希望对你有一定的参考价值。

The largest number could be n of walls.

class Solution {
    public int leastBricks(List<List<Integer>> wall) {
        if (wall.size() == 0) {
            return 0;
        }
        
        int result = wall.size();
        Map<Integer, Integer> sumMap = new HashMap<>();
        for (List<Integer> row : wall) {
            int sum = 0;
            for (int i = 0; i < row.size() - 1; i++) {
                sum += row.get(i);
                sumMap.put(sum, sumMap.getOrDefault(sum, 0) + 1);
            }
        }
        
        for (int value : sumMap.values()) {
            result = Math.min(wall.size() - value, result);
        }
        return result;
    }
}

 

以上是关于LeetCode 554: Brick Wall的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 554: Brick Wall

554. Brick Wall

554. Brick Wall

554. Brick Wall 砖墙

554. Brick Wall最少的穿墙个数

LeetCode554. 砖墙(Java HashMap 统计前缀和)