-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1705: Maximum Number of Eaten Apples
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/problem_1705_maximum_number_of_eaten_apples/greedy_binary_heap.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |