# Find min rate of consumption

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

I know the right way is binary, doesnt need to be explained why its clear to me. ill write my own solution for it later. However, this brute force i wrote is there a way to make more effecient, its executed twice… in the condition of the second while as well is inside of it, I can store computation in hash or there’s no way around it? what else I can do to make more effecient (besides binary) ?

``````/**
* @param {number[]} piles
* @param {number} h
* @return {number}
*/

// Example 2:
// Input: piles = [30,11,23,4,20], h = 5
// Output: 30

// Example 3:
// Input: piles = [30,11,23,4,20], h = 6
// Output: 23

// Example 1:
// Input: piles = [3,6,7,11], h = 8
// Output: 4

//bph bananas per hour | rate of consumption

const minEatingSpeed=([...piles], h, bph)=>{
for(let i=0;i<piles.length;i++){
while(piles[i]>0){
piles[i]-=bph;
h-=1;
}
}
return h
}

const piles=[30,11,23,4,20]
const time=5
let bph=1

while(minEatingSpeed(piles,time,bph)<0){
minEatingSpeed(piles, time, bph+=1)
}

console.log({bph})
``````
1 Like