Skip to content

Commit

Permalink
Create 01-maximum-tastiness-of-candy-basket.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 1, 2023
1 parent ac39502 commit 863000a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 2023/06/01-maximum-tastiness-of-candy-basket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
impl Solution {
pub fn maximum_tastiness(mut price: Vec<i32>, k: i32) -> i32 {
price.sort_unstable();
let (mut left, mut right) = (0, price[price.len() - 1] - price[0]);
let can_x = |x| {
let mut num = 1;
let mut last = price[0];
for i in 1..price.len() {
if price[i] - last >= x {
num += 1;
last = price[i];
}
if num >= k {
return true;
}
}
num >= k
};
while left < right {
let mid = (left + right + 1) / 2;
if can_x(mid) {
left = mid;
} else {
right = mid - 1;
}
}
left
}
}

0 comments on commit 863000a

Please sign in to comment.