diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/koko_eating_bananas.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/koko_eating_bananas.cpp')
-rw-r--r-- | other/koko_eating_bananas.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/other/koko_eating_bananas.cpp b/other/koko_eating_bananas.cpp new file mode 100644 index 0000000..3076169 --- /dev/null +++ b/other/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 +} |