leetcode:554. 砖墙(哈希表)

题目:

leetcode:554. 砖墙(哈希表)_第1张图片

python代码:

class Solution:
    def leastBricks(self, wall: List[List[int]]) -> int:
             m={}
             for x in wall:
                      n=0
                      for y in range(0,len(x)-1):
                           n=n+x[y]
                           m[n]=m.get(n,0)+1
             if(len(m)==0):
                 return len(wall)
             a=sorted(m.items(),key=lambda asd:asd[1],reverse=True)
             return len(wall)-a[0][1]

你可能感兴趣的:(哈希表)