Python, with simple explanation


  • -1

    The optimal width at which the line has to be drawn has to be an integer if not, the line crosses through every brick since the width of each brick is an integer.

    Now we go through each row and count the cumulative widths where each brick is ending and hash them. Find an cum Width which appears maximum number of times thus resulting in a point where it crosses minimum number of bricks.

    class Solution(object):
        def leastBricks(self, wall):
            """
            :type wall: List[List[int]]
            :rtype: int
            """
            if len(wall)==0: return -1
            Counter,cumSum=collections.defaultdict(int),[0]*len(wall)
            for i in range(0,len(wall)):
                for y in wall[i]:
                    cumSum[i]+=y
                    Counter[cumSum[i]]+=1
            Counter[cumSum[0]]=0
            return len(wall)-max(Counter.values())
    

  • -1

    Can anyone explain why this is downvoted? Is there any mistake in the algorithm or the code?


Log in to reply
 

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