Skip to content

Commit

Permalink
Add problem 1705: Maximum Number of Eaten Apples
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 19, 2023
1 parent 20e8959 commit a820b24
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,7 @@ pub mod problem_1700_number_of_students_unable_to_eat_lunch;
pub mod problem_1701_average_waiting_time;
pub mod problem_1702_maximum_binary_string_after_change;
pub mod problem_1704_determine_if_string_halves_are_alike;
pub mod problem_1705_maximum_number_of_eaten_apples;
pub mod problem_1706_where_will_the_ball_fall;
pub mod problem_1710_maximum_units_on_a_truck;
pub mod problem_1711_count_good_meals;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

use std::cmp::Ordering;
use std::collections::binary_heap::PeekMut;
use std::collections::BinaryHeap;

struct Item {
deadline: u16,
count: u16,
}

impl PartialEq for Item {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}

impl Eq for Item {}

impl PartialOrd for Item {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Item {
fn cmp(&self, other: &Self) -> Ordering {
other.deadline.cmp(&self.deadline)
}
}

impl Solution {
pub fn eaten_apples(apples: Vec<i32>, days: Vec<i32>) -> i32 {
let mut queue = BinaryHeap::new();
let mut result = 0;
let mut i = 0;

for (count, day) in apples.into_iter().zip(days) {
queue.push(Item {
deadline: i + day as u16,
count: count as _,
});

while let Some(mut top) = queue.peek_mut() {
if i < top.deadline {
result += 1;

if top.count == 1 {
PeekMut::pop(top);
} else {
top.count -= 1;
}

break;
}

PeekMut::pop(top);
}

i += 1;
}

while let Some(top) = queue.pop() {
let count = top.count.min(top.deadline - i);

result += i32::from(count);

i += count;
}

result
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn eaten_apples(apples: Vec<i32>, days: Vec<i32>) -> i32 {
Self::eaten_apples(apples, days)
}
}

#[cfg(test)]
mod tests {
use super::Item;

#[test]
fn test_item_partial_eq() {
assert!(Item { deadline: 2, count: 3 } == Item { deadline: 2, count: 5 });
}

#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
21 changes: 21 additions & 0 deletions src/problem_1705_maximum_number_of_eaten_apples/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pub mod greedy_binary_heap;

pub trait Solution {
fn eaten_apples(apples: Vec<i32>, days: Vec<i32>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
((&[1, 2, 3, 5, 2] as &[_], &[3, 2, 1, 4, 2] as &[_]), 7),
((&[3, 0, 0, 0, 0, 2], &[3, 0, 0, 0, 0, 2]), 5),
];

for ((apples, days), expected) in test_cases {
assert_eq!(S::eaten_apples(apples.to_vec(), days.to_vec()), expected);
}
}
}

0 comments on commit a820b24

Please sign in to comment.