5 lines Python


  • 0

    Using a counter named count to store the gap positions in each line of the walls. Then the line number of the wall len(wall) minus the maximum value in the count is the answer. Keep in mind we should NOT add two boundary positions into our count.

    class Solution(object):
        def leastBricks(self, wall):
            """
            :type wall: List[List[int]]
            :rtype: int
            """
            for line in wall:
                for i in range(1, len(line)):
                    line[i] += line[i-1]
                    
            count = collections.Counter(x for line in wall for x in line[:-1])
            return len(wall) - max(count.values()) if count else len(wall)
    

Log in to reply
 

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