N
i
piles[i]
H
K
K
K
珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。
H
K
(K
示例 1:
输入: piles = [3,6,7,11], H = 8 输出: 4
输入: piles = [30,11,23,4,20], H = 5 输出: 30
输入: piles = [30,11,23,4,20], H = 6 输出: 23
提示:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
这是一个经典的二分搜索问题,使用经典的模板
class Solution { public: int minEatingSpeed(vector<int>& piles, int H) { int left = 1, right = 100000010; while(left<=right){ int mid = left + (right-left)/2; if(check(piles, H, mid)) right = mid-1; else left = mid + 1; } return left; } bool check(vector<int> piles, int H, int K){ int h = 0; for(int i=0;i<piles.size();i++){ h += (piles[i]+K-1)/K; // 左边等于 piles/k 向上取整 } return h<=H; } };
文章来源: https://blog.csdn.net/wwxy1995/article/details/90145283