-
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 1845: Seat Reservation Manager
- Loading branch information
Showing
4 changed files
with
173 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
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,48 @@ | ||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Reverse; | ||
use std::collections::BinaryHeap; | ||
|
||
struct SeatManager { | ||
heap: BinaryHeap<Reverse<i32>>, | ||
} | ||
|
||
impl SeatManager { | ||
fn new(n: i32) -> Self { | ||
Self { | ||
heap: (1..=n).map(Reverse).collect(), | ||
} | ||
} | ||
|
||
fn reserve(&mut self) -> i32 { | ||
self.heap.pop().unwrap().0 | ||
} | ||
|
||
fn unreserve(&mut self, seat_number: i32) { | ||
self.heap.push(Reverse(seat_number)); | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::SeatManager for SeatManager { | ||
fn new(n: i32) -> Self { | ||
Self::new(n) | ||
} | ||
|
||
fn reserve(&mut self) -> i32 { | ||
self.reserve() | ||
} | ||
|
||
fn unreserve(&mut self, seat_number: i32) { | ||
self.unreserve(seat_number); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::SeatManager>(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/problem_1845_seat_reservation_manager/lazy_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,62 @@ | ||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Reverse; | ||
use std::collections::BinaryHeap; | ||
|
||
struct SeatManager { | ||
heap: BinaryHeap<Reverse<i32>>, | ||
free: i32, | ||
} | ||
|
||
impl SeatManager { | ||
fn new(_: i32) -> Self { | ||
Self { | ||
heap: BinaryHeap::new(), | ||
free: 1, | ||
} | ||
} | ||
|
||
fn reserve(&mut self) -> i32 { | ||
if let Some(Reverse(result)) = self.heap.pop() { | ||
result | ||
} else { | ||
let result = self.free; | ||
|
||
self.free += 1; | ||
|
||
result | ||
} | ||
} | ||
|
||
fn unreserve(&mut self, seat_number: i32) { | ||
if seat_number + 1 == self.free { | ||
self.free -= 1; | ||
} else { | ||
self.heap.push(Reverse(seat_number)); | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::SeatManager for SeatManager { | ||
fn new(n: i32) -> Self { | ||
Self::new(n) | ||
} | ||
|
||
fn reserve(&mut self) -> i32 { | ||
self.reserve() | ||
} | ||
|
||
fn unreserve(&mut self, seat_number: i32) { | ||
self.unreserve(seat_number); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::SeatManager>(); | ||
} | ||
} |
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,62 @@ | ||
pub mod binary_heap; | ||
pub mod lazy_binary_heap; | ||
|
||
pub trait SeatManager { | ||
fn new(n: i32) -> Self; | ||
fn reserve(&mut self) -> i32; | ||
fn unreserve(&mut self, seat_number: i32); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::SeatManager; | ||
|
||
enum Operation { | ||
Reserve(i32), | ||
Unreserve(i32), | ||
} | ||
|
||
pub fn run<M: SeatManager>() { | ||
let test_cases = [ | ||
( | ||
5, | ||
&[ | ||
Operation::Reserve(1), | ||
Operation::Reserve(2), | ||
Operation::Unreserve(2), | ||
Operation::Reserve(2), | ||
Operation::Reserve(3), | ||
Operation::Reserve(4), | ||
Operation::Reserve(5), | ||
Operation::Unreserve(5), | ||
] as &[_], | ||
), | ||
( | ||
4, | ||
&[ | ||
Operation::Reserve(1), | ||
Operation::Unreserve(1), | ||
Operation::Reserve(1), | ||
Operation::Reserve(2), | ||
Operation::Reserve(3), | ||
Operation::Unreserve(2), | ||
Operation::Reserve(2), | ||
Operation::Unreserve(1), | ||
Operation::Reserve(1), | ||
Operation::Unreserve(2), | ||
], | ||
), | ||
]; | ||
|
||
for (n, operations) in test_cases { | ||
let mut seat_manager = M::new(n); | ||
|
||
for operation in operations { | ||
match *operation { | ||
Operation::Reserve(expected) => assert_eq!(seat_manager.reserve(), expected), | ||
Operation::Unreserve(seat_number) => seat_manager.unreserve(seat_number), | ||
} | ||
} | ||
} | ||
} | ||
} |