aboutsummaryrefslogtreecommitdiff
path: root/koko_eating_bananas.cpp
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /koko_eating_bananas.cpp
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'koko_eating_bananas.cpp')
-rw-r--r--koko_eating_bananas.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/koko_eating_bananas.cpp b/koko_eating_bananas.cpp
new file mode 100644
index 0000000..3076169
--- /dev/null
+++ b/koko_eating_bananas.cpp
@@ -0,0 +1,30 @@
+// 875. Koko Eating Bananas
+#include "leetcode.h"
+
+// return minimum integer 'k' such that koko can eat all bananas within 'h'
+// hours
+
+class Solution {
+public:
+ int minEatingSpeed(vector<int> &piles, int h) {
+ 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;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> p1 = {3, 6, 7, 11}, p2_3 = {30, 11, 23, 4, 20};
+ printf("%d\n", obj.minEatingSpeed(p1, 8)); // expect: 4
+ printf("%d\n", obj.minEatingSpeed(p2_3, 5)); // expect: 30
+ printf("%d\n", obj.minEatingSpeed(p2_3, 6)); // expect: 23
+}