반응형
https://leetcode.com/problems/koko-eating-bananas/
Koko Eating Bananas - LeetCode
Can you solve this real interview question? 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 sp
leetcode.com
이진 탐색을 이용하여 풀 수 있었습니다.
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
int l = 1, r = 1000000000;
while(l <= r) {
int m = (l + r) / 2;
if(chk(piles, h, m)) {
r = m - 1;
} else {
l = m + 1;
}
}
return r + 1;
}
bool chk(vector<int>& piles, int h, int k) {
for(int pile : piles) {
h -= pile / k + ((pile % k) != 0);
if(h < 0) return false;
}
return true;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 382. Linked List Random Node (0) | 2023.03.10 |
---|---|
LeetCode 142. Linked List Cycle II (0) | 2023.03.09 |
LeetCode 2187. Minimum Time to Complete Trips (0) | 2023.03.07 |
LeetCode 1539. Kth Missing Positive Number (0) | 2023.03.06 |
LeetCode 1345. Jump Game IV (0) | 2023.03.05 |