|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +CREATED AT: 2022年11月23日 |
| 4 | + |
| 5 | +URL: https://leetcode.com/problems/maximum-number-of-balls-in-a-box/ |
| 6 | + |
| 7 | +GITHUB: https://github.com/Jiezhi/myleetcode |
| 8 | + |
| 9 | +FileName: 1742-MaximumNumberOfBallsInABox |
| 10 | + |
| 11 | +Difficulty: Easy |
| 12 | + |
| 13 | +Desc: |
| 14 | + |
| 15 | +Tag: |
| 16 | + |
| 17 | +See: |
| 18 | + |
| 19 | +""" |
| 20 | +from tool import * |
| 21 | + |
| 22 | + |
| 23 | +class Solution: |
| 24 | + def countBalls(self, lowLimit: int, highLimit: int) -> int: |
| 25 | + """ |
| 26 | + Runtime: 490 ms, faster than 92.16% |
| 27 | + Memory Usage: 13.8 MB, less than 97.97% |
| 28 | + 1 <= lowLimit <= highLimit <= 10^5 |
| 29 | + """ |
| 30 | + |
| 31 | + def get_box(num) -> int: |
| 32 | + ret = 0 |
| 33 | + while num >= 10: |
| 34 | + num, left = divmod(num, 10) |
| 35 | + ret += left |
| 36 | + return ret + num |
| 37 | + |
| 38 | + cnt = Counter() |
| 39 | + for num in range(lowLimit, highLimit + 1): |
| 40 | + cnt[get_box(num)] += 1 |
| 41 | + return cnt.most_common(1)[0][1] |
| 42 | + |
| 43 | + |
| 44 | +def test(): |
| 45 | + assert Solution().countBalls(lowLimit=1, highLimit=10) == 2 |
| 46 | + assert Solution().countBalls(lowLimit=5, highLimit=15) == 2 |
| 47 | + assert Solution().countBalls(lowLimit=19, highLimit=28) == 2 |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + test() |
0 commit comments