Koko Eating Bananas - LeetCode 875
LeetCode 875: Koko Eating Bananas - C++ Solution Explained In this video, I solve LeetCode problem #875 - Koko Eating Bananas using C++ with a binary search on answer approach! ⏱️ Time Complexity: O(n * log(max(piles))) 💾 Space Complexity: O(1) 📌 Problem Overview: Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours. Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour. Return the minimum integer k such that she can eat all the bananas within h hours. Example: piles = [3,6,7,11], h = 8 Output: 4 (at speed 4, she can finish all piles in 8 hours) 💡 Solution Approach: - Binary search on the answer (eating speed k) - Search range: 1 to max(piles) - For each mid value, check if Koko can finish all bananas in h hours - Calculate hours needed: sum of ceil(pile[i] / k) for all piles - If hours less than (or equal) h, try smaller speed (move right = mid) - Otherwise, need faster speed (move left = mid + 1) - Classic "binary search on answer" pattern! 🔗 LeetCode Problem Link: https://leetcode.com/problems/koko-eating-bananas/ 📚 Important pattern: Binary search on answer range - great for optimization problems! ⭐ If this helped you, please like and subscribe for more coding solutions! #leetcode #cpp #dsa #coding #programming #binarysearch #algorithms #interview #faang #mediumleetcode #binarysearchonanswer
Download
0 formatsNo download links available.