Skip to content

Commit

Permalink
Merge pull request neetcode-gh#891 from MaratKhakim/875-Koko-Eating-Kt
Browse files Browse the repository at this point in the history
Kotlin: 875. Koko Eating Bananas
  • Loading branch information
Ahmad-A0 authored Aug 23, 2022
2 parents fc9bd24 + dac2aa6 commit 0af9266
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions kotlin/875-Koko-Eating-Bananas.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
fun minEatingSpeed(piles: IntArray, h: Int): Int {
var left = 1
var right = 1

for (pile in piles) {
right = Math.max(right, pile)
}

while (left < right) {
val mid = left + (right - left)/2

if (canEat(piles, h, mid)) {
right = mid
} else {
left = mid+1
}
}

return left
}

fun canEat(piles: IntArray, h: Int, speed: Int): Boolean {
var sum = 0

for (pile in piles) {
sum += Math.ceil(pile/speed.toDouble()).toInt()

if (sum > h)
return false
}

return true
}
}

0 comments on commit 0af9266

Please sign in to comment.