Skip to content

Commit 4ed6e87

Browse files
committed
Update leetcode 875 example with comments and with differnt approachs
1 parent 5fe488e commit 4ed6e87

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

Cpp/kokoEatingBananas.cpp

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
#include <iostream>
22
#include <vector>
33
#include <algorithm>
4+
#include <cmath>
5+
6+
// leetcode 875. Koko Eating Bananas
7+
8+
// clasic binary search example
9+
10+
// Piles = [3, 6, 7, 11] How many number of bananas should be ate per hour to finish in H hour all piles.
11+
// we can eat 1 banana per hour then total hours will be 27 .
12+
// we can eat 2 bananas per hour then total hours will be 15 hours.
13+
// maximum number of bananas per hour can be the std::max_element(piles.begin(), piles.end())
14+
// in this case, 11. If 11 bananas is ate per one hour then all piles will finish in 4 hours.
15+
/*
16+
with 1 bananas per hour, all piles will finish in 27 hours
17+
with 2 bananas per hour, all piles will finish in 15 hours
18+
with 3 bananas per hour, all piles will finish in 10 hours
19+
with 4 bananas per hour, all piles will finish in 8 hours
20+
with 5 bananas per hour, all piles will finish in 8 hours
21+
with 6 bananas per hour, all piles will finish in 6 hours
22+
with 7 bananas per hour, all piles will finish in 5 hours
23+
with 8 bananas per hour, all piles will finish in 5 hours
24+
with 9 bananas per hour, all piles will finish in 5 hours
25+
with 10 bananas per hour, all piles will finish in 5 hours
26+
with 11 bananas per hour, all piles will finish in 4 hours
27+
28+
As it is seen, if number of bananas to be ate for per hour is increasing then the
29+
total time to finish all piles is decreasing. This is called monotonic function.
30+
With monotonic function we can use binary search to find desired value.
31+
(brute force will take much time)
32+
33+
simply we have array 1 to 11, it is needed to choose correct number according to the desired hour.
34+
*/
435

536
class Solution {
6-
public:
37+
738
long long findSuitableHour(const std::vector<int>& piles, int target)
839
{
940
long long currPile = 0;
@@ -24,7 +55,19 @@ class Solution {
2455
}
2556
return hours;
2657
}
27-
58+
59+
long long hoursToFinish(std::vector<int>& piles, int bananasForOneHour)
60+
{
61+
long long totalHour {0};
62+
63+
for(const auto& pile : piles)
64+
{
65+
totalHour += ceil(pile/(double)bananasForOneHour);
66+
}
67+
return totalHour;
68+
}
69+
70+
public:
2871
int minEatingSpeed(std::vector<int>& piles, int h) {
2972

3073
int l=1, r= *max_element(piles.begin(),piles.end());;
@@ -39,7 +82,32 @@ class Solution {
3982
}
4083

4184
return l;
42-
}
85+
}
86+
87+
int minEatingSpeed_2(std::vector<int>& piles, int hour)
88+
{
89+
int max {*std::max_element(piles.begin(), piles.end())};
90+
int low {1};
91+
92+
int ans {0};
93+
94+
while(low <= max)
95+
{
96+
int mid = low + (max - low)/2;
97+
if(hoursToFinish(piles, mid) <= hour)
98+
{
99+
ans = mid;
100+
max = mid - 1;
101+
}
102+
else
103+
{
104+
low = mid + 1;
105+
}
106+
}
107+
108+
return ans;
109+
}
110+
43111
int minEatingSpeedBruteForce(const std::vector<int>& piles, int target) {
44112

45113
for(unsigned int j = 1; j <= *max_element(piles.begin(),piles.end()); j++)
@@ -77,6 +145,7 @@ int main()
77145
{
78146
std::vector<int> vec {30,11,23,4,20};
79147
std::cout<<Solution{}.minEatingSpeed(vec, 6)<<std::endl;
148+
std::cout<<Solution{}.minEatingSpeed_2(vec, 6)<<std::endl;
80149

81150
std::cout<<Solution{}.minEatingSpeedBruteForce(vec, 6)<<std::endl;
82151

0 commit comments

Comments
 (0)