Leetcode Practice
LC 875. Koko Eating Bananas
- Last updated
- Reading time
- 1 min read
The problem
Time
Solution -function minEatingSpeed(piles: number[], h: number): number {
let minSpeed = 1
let maxSpeed = Math.max(...piles)
while (minSpeed <= maxSpeed) {
let midSpeed = Math.floor((minSpeed + maxSpeed) / 2)
let midSpeedHours = piles.reduce((acc, x) => (acc += Math.ceil(x / midSpeed)), 0)
if (midSpeedHours > h) {
minSpeed = midSpeed + 1
} else {
maxSpeed = midSpeed - 1
}
}
return minSpeed
}