554. Brick Wall (Medium)

There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to draw a vertical line from the top to the bottom and cross the least bricks.

The brick wall is represented by a list of rows. Each row is a list of integers representing the width of each brick in this row from left to right.

If your line go through the edge of a brick, then the brick is not considered as crossed. You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks.

You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.

Example:

Input: 
[[1,2,2,1],
 [3,1,2],
 [1,3,2],
 [2,4],
 [3,1,2],
 [1,3,1,1]]
Output: 2
Explanation:

Note:

  1. The width sum of bricks in different rows are the same and won't exceed INT_MAX.
  2. The number of bricks in each row is in range [1,10,000]. The height of wall is in range [1,10,000]. Total number of bricks of the wall won't exceed 20,000.
Solution 1: HashMap O(n); O(w) nn is the total number of bricks in a wall, w is width of wall

使用一个哈希表来建立每一个断点的长度和其出现频率之间的映射,这样只要我们从断点频率出现最多的地方劈墙,损坏的板砖一定最少

    /**
     * HashMap O(n); O(w) nn is the total number of bricks in a wall, w is width of wall
     * map will contain atmost w entries, where w refers to the width of the wall.
     * We want to cut from the edge of the most common location among all the levels,
     * hence use a map to record the locations and their corresponding frequency of occurrence
     * key是当前砖块的右边距离起点的距离,value是frequency of occurrence
     * key is the distance from the left of wall to the right of current brick.
     */
    public int leastBricks(List<List<Integer>> wall) {
        if (wall == null) {
            return -1;
        }

        Map<Integer, Integer> map = new HashMap<>();
        int count = 0;
        for (List<Integer> list : wall) {
            int len = 0;
            for (int i = 0; i < list.size() - 1; i++) {
                len += list.get(i);
                map.put(len, map.getOrDefault(len, 0) + 1);
                count = Math.max(count, map.get(len));
            }
        }
        return wall.size() - count;
    }
Solution 2: min heap
    public int leastBricksB(List<List<Integer>> wall) {
        int R = wall.size(), min = R;
        if (R == 1 && wall.get(0).size() > 1) return 0;
        // [0: end, 1: row, 2: col]
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[0] - b[0]));
        for (int i = 0; i < R; i++) {
            pq.add(new int[]{wall.get(i).get(0), i, 0});
        }

        while (!pq.isEmpty()) {
            int end = pq.peek()[0], count = 0;
            while (!pq.isEmpty() && pq.peek()[0] == end) {
                count++;
                int[] brick = pq.poll();
                if (brick[2] < wall.get(brick[1]).size() - 1) {
                    pq.add(new int[]{end + wall.get(brick[1]).get(brick[2] + 1), brick[1], brick[2] + 1});
                }
            }
            if (!pq.isEmpty()) {
                min = Math.min(min, R - count);
            }
        }
        return min;
    }
Follow Up:

1.这面墙的高度很小,但是宽度很大,每一行可能有特别特别多的砖,问你怎么办。http://www.1point3acres.com/bbs/thread-268942-1-1.html

只需要有墙高度个数的pointer就可以了,比较当前指向的值有没有overlap,然后increment最小的那个(3个pointer中最小的那个)

TODO不太理解。明白了,比如墙的高度是3,用3个变量记录当前brick右边距离起点的位置,用一个变量记录最大gap数(在同一位置)。

results matching ""

    No results matching ""