Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1987 from nirajvenkat/patch-6
Browse files Browse the repository at this point in the history
Create 0875-koko-eating-bananas.rs
  • Loading branch information
tahsintunan authored Jan 11, 2023
2 parents 973981e + b99d6ef commit 9fdbcd2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rust/0875-koko-eating-bananas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::cmp::Ordering::{Equal, Less, Greater};

impl Solution {
pub fn min_eating_speed(piles: Vec<i32>, h: i32) -> i32 {
let max_piles = *piles.iter().max().unwrap() as usize;
let (mut l, mut r) = (1, max_piles);
let mut k = max_piles;

while l <= r {
let m = l + (r - l) / 2;
let hrs: usize = piles.iter()
.map(|&num_bananas| ((num_bananas - 1) as usize / m) + 1)
.sum();

match hrs.cmp(&(h as usize)) {
Less | Equal => {
k = k.min(m);
r = m - 1;
},
Greater => l = m + 1,
}
}

k as i32
}
}

0 comments on commit 9fdbcd2

Please sign in to comment.