We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目链接: https://leetcode-cn.com/problems/koko-eating-bananas
难度: Medium 标签: 数组 二分查找
Medium
数组
二分查找
The text was updated successfully, but these errors were encountered:
Difficulty: 中等
Related Topics: 数组, 二分查找
珂珂喜欢吃香蕉。这里有 n 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 h 小时后回来。
n
i
piles[i]
h
珂珂可以决定她吃香蕉的速度 k (单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 k 根。如果这堆香蕉少于 k 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。
k
珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。
返回她可以在 h 小时内吃掉所有香蕉的最小速度 k(k 为整数)。
示例 1:
输入:piles = [3,6,7,11], h = 8 输出:4
示例 2:
输入:piles = [30,11,23,4,20], h = 5 输出:30
示例 3:
输入:piles = [30,11,23,4,20], h = 6 输出:23
提示:
思路:
min =1 (banana / h)
max = max(piles)
mid
Language: JavaScript
/** * @param {number[]} piles * @param {number} h * @return {number} */ var minEatingSpeed = function(piles, h) { let [min, max] = [1, Math.max(...piles)]; while (min < max) { const mid = (min + max) >>> 1; if (timeLessThenH(h, mid, piles)) max = mid; else min = mid + 1; } return min; }; // 此处要注意用Math.ceil()来取得所需时间的上限 const timeLessThenH = (h, speed, arr) => arr.reduce((time, num) => time += Math.ceil(num / speed), 0) <= h;
Sorry, something went wrong.
No branches or pull requests
题目链接: https://leetcode-cn.com/problems/koko-eating-bananas
难度:
Medium
标签:
数组
二分查找
The text was updated successfully, but these errors were encountered: