Skip to content

Commit

Permalink
Add problem 1845: Seat Reservation Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 18, 2023
1 parent 524ab16 commit 6207ffe
Show file tree
Hide file tree
Showing 4 changed files with 173 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 @@ -1419,6 +1419,7 @@ pub mod problem_1833_maximum_ice_cream_bars;
pub mod problem_1837_sum_of_digits_in_base_k;
pub mod problem_1839_longest_substring_of_all_vowels_in_order;
pub mod problem_1844_replace_all_digits_with_characters;
pub mod problem_1845_seat_reservation_manager;

#[cfg(test)]
mod test_utilities;
48 changes: 48 additions & 0 deletions src/problem_1845_seat_reservation_manager/binary_heap.rs
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 src/problem_1845_seat_reservation_manager/lazy_binary_heap.rs
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>();
}
}
62 changes: 62 additions & 0 deletions src/problem_1845_seat_reservation_manager/mod.rs
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),
}
}
}
}
}

0 comments on commit 6207ffe

Please sign in to comment.