Python solution with dict


  • 0
    K

    The corner case here is when there is only one brick at each height.
    We can use a dict to record where there is a gap and go through the line with most gap.

            if not wall or not wall[0]:
                return 0
            h,w = len(wall),len(wall[0])
            dic = {}
            for i in xrange (h):
                cnt = 0
                for num in wall[i]:
                    cnt += num
                    dic[cnt] = dic.get(cnt,0) + 1
            del dic[sum(wall[0])]
            if not dic.values():
                return h
            return h - max(dic.values())
    

    Please tell me if you have better idea or I am wrong. Thanks~


Log in to reply
 

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