반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 875 | Koko Eating Bananas | Python 본문
728x90
반응형
문제 링크: https://leetcode.com/problems/koko-eating-bananas/
문제
Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.
Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
Return the minimum integer k such that she can eat all the bananas within h hours.
조건
- 1 <= piles.length <= $10^4$
- piles.length <= h <= $10^9$
- 1 <= piles[i] <= $10^9$
내 풀이
이 문제는 보자마자 이분탐색으로 풀어야겠다는 생각이 들었다.
왜냐하면, 1부터 max(piles)까지 하나하나 적용해보면 풀 수 있지만, 그렇게되면 시간이 많이 걸리기 때문이다.
코드
from math import ceil
class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
start, end = 1, max(piles)
mid = start
while start < end:
hours = 0
for pile in piles:
hours += ceil(pile/mid)
if hours > h:
start = mid + 1
else:
end = mid
mid = start + (end-start) // 2
return mid
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 520 | Detect Capital | Python (0) | 2022.01.24 |
---|---|
[백준] 3190번 | 뱀 | C++ (0) | 2022.01.20 |
[LeetCode] 142 | Linked List Cycle II | Python (0) | 2022.01.19 |
[백준] 2839번 | 설탕 배달 | C (0) | 2022.01.17 |
[LeetCode] 290 | Word Pattern | Python (0) | 2022.01.17 |
Comments