Code working too slow(Please explain reasons)(https://leetcode.com/problems/koko-eating-bananas/)
class Solution {
    public int minEatingSpeed(int[] piles, int H) {
     int min,mid = 0,max;min=0;max=piles[0];int copy=H;

        for(int i:piles)
        {
            max= Math.max(max,i);
        }

        while(min<max) {

            mid = min + (max - min) / 2;H=copy;
            if(mid==0)
            {mid=1;break;}


            for (int i = 0; i < piles.length; i++) {
                if (piles[i] <= mid)
                    H--;
                else {
                    if (piles[i] % mid > 0)
                        H = H - ((piles[i] / mid) + 1);
                    else
                        H = H - piles[i]/mid;
                    if(H<0)
                        break;
                }
            }

            if(H>0)
                max=mid;
            else if(H==0)
                max=mid;
            else
                min=mid+1;



        }
        if(min==max)
            mid=min+(max-min)/2;

        

return mid;
    }
}
Comments (0)