HanchengZhao / algorithm-notes

0 stars 0 forks source link

554. Brick Wall-Jun 14, 2017 #5

Open YeWang0 opened 7 years ago

YeWang0 commented 7 years ago

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.

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

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.
HanchengZhao commented 7 years ago

Brute Force way

My first idea was to go through each column of the whole wall, but it would lead to TLE, since the length of a brick could be very huge:

class Solution(object):
    def leastBricks(self, wall):
        least = len(wall)
        while wall[0] and not (wall[0][0] == 1 and len(wall[0]) == 1):
            count = 0
            for row in wall:
                if row[0] == 1:
                    row.pop(0)
                else:
                    row[0] -= 1
                    count += 1
            least = min(least, count)
        return least

Using hashtable

Then I realized I only needed to count the occurrence of each brick space and used the height of wall to subtract the max occurrence of the space

class Solution(object):
    def leastBricks(self, wall):
        spaceMap = dict()
        for row in wall:
            spacelen = 0
            for brick in row[:-1]: # avoid counting wall edge
                spacelen += brick
                if spacelen not in spaceMap:
                    spaceMap[spacelen] = 1
                else:
                    spaceMap[spacelen] += 1
        least = len(wall) - max(spaceMap.values())
        return least

Time Complexity: O(n), n for number of bricks Space Complexity: O(n)

GorillaSX commented 7 years ago

Thought:

The basic idea is performing prefix sum for each row, then for each row, at each gap, we could get the distance to the left edge. We also need to count the number of each distance appeared, because each distance cannot appear twice at each row, then answer should be : the number of row - the maximum number.

code:

class Solution {
public:
    int leastBricks(vector<vector<int>>& wall) {
        int r = wall.size();
        unordered_map<int,int> mymap;
        int maxval = 0;
        for(int row = 0;row < r;row++)
        {
            for(int col = 0;col < wall[row].size();col++)
            {
                if(col != wall[row].size() - 1)
                {
                    if(col > 0)
                        wall[row][col] = wall[row][col] + wall[row][col-1];
                    mymap[wall[row][col]]++;
                    maxval = max(maxval,mymap[wall[row][col]]);
                }
            }
        }
        return r - maxval;
    }
};
HanchengZhao commented 7 years ago

@GorillaSX priority_queue?

GorillaSX commented 7 years ago

I have updated the comment. The priority_queue is used to record the number of distance, then I found it's unnecessary, we don't need to consider the key, just the maximum number is what we needed.

YeWang0 commented 7 years ago

Similar to merge K linked list