diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/koko\345\201\267\351\246\231\350\225\211.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/koko\345\201\267\351\246\231\350\225\211.md" index cc880dd268762c333e98b2fc7fcbfb7374f9b234..635291c68c8c7b2a1df26efb4c82c0c174787e18 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/koko\345\201\267\351\246\231\350\225\211.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/koko\345\201\267\351\246\231\350\225\211.md" @@ -169,4 +169,45 @@ for (int i = 0; i < n; i++)

-======其他语言代码====== \ No newline at end of file +======其他语言代码====== + +[tonytang731](https://https://github.com/tonytang731) 提供 Python3 代码: +```python +import math + +class Solution: + def minEatingSpeed(self, piles, H): + # 初始化起点和终点, 最快的速度可以一次拿完最大的一堆 + start = 1 + end = max(piles) + + # while loop进行二分查找 + while start + 1 < end: + mid = start + (end - start) // 2 + + # 如果中点所需时间大于H, 我们需要加速, 将起点设为中点 + if self.timeH(piles, mid) > H: + start = mid + # 如果中点所需时间小于H, 我们需要减速, 将终点设为中点 + else: + end = mid + + # 提交前确认起点是否满足条件,我们要尽量慢拿 + if self.timeH(piles, start) <= H: + return start + + # 若起点不符合, 则中点是答案 + return end + + + + def timeH(self, piles, K): + # 初始化时间 + H = 0 + + #求拿每一堆需要多长时间 + for pile in piles: + H += math.ceil(pile / K) + + return H +```