[LeetCode]554. Brick Wall

https://leetcode.com/problems/brick-wall/#/description


一个二维的墙,找出一条int位置的竖线,能穿过最少的墙




反向思考,找出空隙最多的index,而不是找墙最少的index,否则有可能超内存

public class Solution {
    public int leastBricks(List> wall) {
        if (wall == null) {
            return 0;
        }
        HashMap map = new HashMap();
        int cnt = 0;
        for (List w : wall) {
            int idx = 0;
            for (int i = 0; i < w.size() - 1; i++) {
                idx += w.get(i);
                map.put(idx, map.getOrDefault(idx, 0) + 1);
                cnt = Math.max(map.get(idx), cnt);
            }
        }
        return wall.size() - cnt;
    }
}


你可能感兴趣的:(LeetCode)