-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path73.minEatingSpeed.cpp
More file actions
executable file
·66 lines (61 loc) · 1.91 KB
/
Copy path73.minEatingSpeed.cpp
File metadata and controls
executable file
·66 lines (61 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
@filename 73.minEatingSpeed.cpp
@author caonan
@date 2022-05-06 16:06:49
@reference 剑指offer专项
@url https://leetcode-cn.com/problems/nZZqjQ
@brief 狒狒喜欢吃香蕉。这里有 n 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 h 小时后回来。
狒狒可以决定她吃香蕉的速度 k (单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 k 根。如果这堆香蕉少于 k
根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉,下一个小时才会开始吃另一堆的香蕉。
狒狒喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。
返回她可以在 h 小时内吃掉所有香蕉的最小速度 k(k 为整数)
*/
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution
{
public:
int minEatingSpeed(vector<int>& piles, int h)
{
int max = 0;
for (auto n : piles) {
max = std::max(max, n);
}
int l = 1;
int r = max;
auto getHours = [&piles](int eat_speed) -> int {
int hours = 0;
for (auto n : piles) {
//向上取整
hours += n % eat_speed ? n / eat_speed + 1 : n / eat_speed;
}
return hours;
};
while (l <= r) {
int mid = l + (r - l) / 2;
if (getHours(mid) <= h) {
if (mid == 1 || getHours(mid - 1) > h) {
return mid;
}
r = mid - 1;
} else {
l = mid + 1;
}
}
return 0;
}
};
int main() { return 0; }