爆料!Facebook 80%的算法题是原题!
最近,不少面过Facebook并且拿到offer的同学爆料,Facebook 80%的算法题都来源于领扣的原题。多刷多练,找到解题思路,FB的offer简直手到擒来!
话不多说,领扣🐱送上FB这道经典原题!
LintCode 1310
爱吃香蕉的珂珂
题目描述 珂珂喜欢吃香蕉。这里有 N 堆香蕉,第 i 堆中有 piles[i]
根香蕉。警卫已经离开了,将在 H
小时后回来。 珂珂可以决定她吃香蕉的速度 K
(单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 K 根。如果这堆香蕉少于 K 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。 珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。 返回她可以在 H 小时内吃掉所有香蕉的最小速度 K
(K 为整数)。 1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
题目描述
piles[i]
根香蕉。警卫已经离开了,将在 H
小时后回来。K
(单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 K 根。如果这堆香蕉少于 K 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。K
(K 为整数)。扫码免费做题 ↓↓↓
样例 1 输入: piles = [3,6,7,11], H = 8
输出: 4
解释:6->4*2,7->4*2,11->4*3,3->4*1
样例 2 输入: piles = [30,11,23,4,20], H = 5
输出: 30
解释:4->30*1,11->30*1,20->30*1,23->30*1,30->30*1
解题思路 采用二分的解法 源代码 public class Solution {
/**
* @param piles: an array
* @param H: an integer
* @return: the minimum integer K
*/
public int minEatingSpeed(int[] piles, int H) {
// Write your code here
int l = 1, r = 1000000000;
while (l < r) {
int m = (l + r) / 2, total = 0;
for (int p : piles)
total += (p + m - 1) / m;
if (total > H)
l = m + 1;
else
r = m;
}
return l;
}
}
点击【阅读原文】,查看领扣原题。
阅读原文 输入: piles = [3,6,7,11], H = 8
输出: 4
解释:6->4*2,7->4*2,11->4*3,3->4*1
输入: piles = [30,11,23,4,20], H = 5
输出: 30
解释:4->30*1,11->30*1,20->30*1,23->30*1,30->30*1
解题思路
源代码
public class Solution {
/**
* @param piles: an array
* @param H: an integer
* @return: the minimum integer K
*/
public int minEatingSpeed(int[] piles, int H) {
// Write your code here
int l = 1, r = 1000000000;
while (l < r) {
int m = (l + r) / 2, total = 0;
for (int p : piles)
total += (p + m - 1) / m;
if (total > H)
l = m + 1;
else
r = m;
}
return l;
}
}
关键词
算法
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。