leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

Bug Report for eating-bananas

Open giacomib opened this issue 2 months ago • 0 comments

Bug Report for https://neetcode.io/problems/eating-bananas

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

The following code pass all the tests on neetcode, but does not on leetcode. I notice that removing the int cast does make the code pass all the tests on both neetcode and leetcode

    public boolean isValid(int[] piles, int h, int k) {
        int sum = 0;
        for(int i = 0; i < piles.length; i++) {
            sum +=  (int) Math.ceil((double) piles[i] / k);
        }
        if(sum <= h)
            return true;
        return false;
    }

all the code:

class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        int max = max(piles);
        int l = 1;
        int r = max;
        int res = max;
        
        // middle
        int k = 1; 
        
        while(l <= r) {
            k = l + ((r - l) / 2);
            boolean valid = isValid(piles, h, k);
            if(valid) {
                res = k;
                r = k - 1;
            } else {
                l = k + 1;
            }
        }
        return res;
    }

    public int max(int[] piles) {
        int max = 0;
        if(piles.length == 0)
            return -1;
        for(int i = 0; i < piles.length; i++){
            if(piles[i] > max)
                max = piles[i];
        }
        return max;
    }

    public boolean isValid(int[] piles, int h, int k) {
        int sum = 0;
        for(int i = 0; i < piles.length; i++) {
            sum +=  (int) Math.ceil((double) piles[i] / k);
        }
        if(sum <= h)
            return true;
        return false;
    }
}

giacomib avatar Sep 25 '25 08:09 giacomib