1
1
#include < iostream>
2
2
#include < vector>
3
3
#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
+ */
4
35
5
36
class Solution {
6
- public:
37
+
7
38
long long findSuitableHour (const std::vector<int >& piles, int target)
8
39
{
9
40
long long currPile = 0 ;
@@ -24,7 +55,19 @@ class Solution {
24
55
}
25
56
return hours;
26
57
}
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:
28
71
int minEatingSpeed (std::vector<int >& piles, int h) {
29
72
30
73
int l=1 , r= *max_element (piles.begin (),piles.end ());;
@@ -39,7 +82,32 @@ class Solution {
39
82
}
40
83
41
84
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
+
43
111
int minEatingSpeedBruteForce (const std::vector<int >& piles, int target) {
44
112
45
113
for (unsigned int j = 1 ; j <= *max_element (piles.begin (),piles.end ()); j++)
@@ -77,6 +145,7 @@ int main()
77
145
{
78
146
std::vector<int > vec {30 ,11 ,23 ,4 ,20 };
79
147
std::cout<<Solution{}.minEatingSpeed (vec, 6 )<<std::endl;
148
+ std::cout<<Solution{}.minEatingSpeed_2 (vec, 6 )<<std::endl;
80
149
81
150
std::cout<<Solution{}.minEatingSpeedBruteForce (vec, 6 )<<std::endl;
82
151
0 commit comments