AlgoMaster Logo

Koko Eating Bananas

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force Solution

Intuition:

In this approach, we try every possible eating speed k starting from 1 until we find a speed that allows Koko to eat all the bananas within h hours. For each speed, we simulate the eating process and check if Koko can finish all bananas within h hours.

Code:

2. Binary Search Solution

Intuition:

Using binary search, we efficiently narrow down the range of Koko's possible eating speeds. Instead of checking each speed one by one, we continuously adjust our search range based on whether the current mid speed allows Koko to eat all bananas in h hours. The goal is to find the minimal speed k that works.

Code: