***Koko Eating Bananas(Leetcode 875)

Problem Link: https://leetcode.com/problems/koko-eating-bananas/

import math
class Solution:
    def minEatingSpeed(self, piles: List[int], h: int) -> int:
        
        def hourstaken(piles,maxSpeed,hours):
            c=0
            for i in piles:
                c=c+math.ceil(i/maxSpeed)
            return c
                
            
        def bsearch(piles,low,high):
            
            if(low<=high):
                mid=(low+high)//2
                hourstook=hourstaken(piles,mid,h)
                if(hourstook<=h):
                    self.ans=min(self.ans,mid)
                    bsearch(piles,low,mid-1)
                else:
                    bsearch(piles,mid+1,high)
                
            return self.ans
                    
                
        n=len(piles)
        low=1
        high=max(piles)
        self.ans=float('inf')
        self.ans=bsearch(piles,low,high)
        return self.ans

Last updated