Leetcode 1742. Maximum Number of Balls in a Box [Python]
作者:互联网
暴力求解,算出每个球需要被丢去哪个盒子。然后用一个堆来确定求的数量最多是多少。
class Solution:
def countBalls(self, lowLimit: int, highLimit: int) -> int:
dic = collections.defaultdict(int)
for num in range(lowLimit, highLimit+1):
idx = self.counttoone(num)
dic[idx] += 1
heap = []
for idx, count in dic.items():
heapq.heappush(heap,(-count))
return -1* heapq.heappop(heap)
def counttoone(self, num):
res = 0
stnum = str(num)
for i in stnum:
res += int(i)
return res
标签:Box,Balls,idx,Python,res,self,int,num,heap 来源: https://blog.csdn.net/sinat_30403031/article/details/122780197