Skip to main content

Command Palette

Search for a command to run...

Koko Eating Baacnanas

Published
2 min read
Koko Eating Baacnanas
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Koko loves to eat bananas. There are n piles of bananas, the i<sup>th</sup> 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.

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

Return the minimum integer k such that she can eat all the bananas within h hours.

LeetCode Problem - 875: Link | Click Here

import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;

class Solution {
    // Method to find the minimum eating speed
    public int minEatingSpeed(int[] piles, int h) {

        // Get summary statistics of the array using Java streams
        IntSummaryStatistics sb = IntStream.of(piles).summaryStatistics();

        // Set the initial search range for the eating speed
        int high = sb.getMax();
        int low = 1;

        // Binary search loop to find the optimal eating speed
        while(low <= high){
            // Calculate the middle value of the current search range
            int mid = (low + high) / 2;

            // Calculate the total hours required for the current eating speed
            int speed = minEat(piles, mid);

            // Adjust the search range based on the calculated total hours
            if(speed < h){
                high = mid - 1;  // Reduce the eating speed
            }
            else{
                low = mid + 1;   // Increase the eating speed
            }
        }

        // Return the lowest eating speed that satisfies the given hours
        return low;
    }

    // Method to calculate the total hours required for a given eating speed
    public int minEat(int[] arr, int h){
        int totalHour = 0;
        // Calculate total hours using the formula: totalHour += ceil(arr[i] / h)
        for(int i = 0; i < arr.length; i++){
            totalHour += Math.ceil((double) arr[i] / h);
        }
        return totalHour;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.