暴力求解,算出每个球需要被丢去哪个盒子。然后用一个堆来确定求的数量最多是多少。
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