Skip to content

Instantly share code, notes, and snippets.

@mdrokz
Created May 29, 2023 05:22
Show Gist options
  • Save mdrokz/42f8741b7f369adf6ff4a487caeb97fd to your computer and use it in GitHub Desktop.
Save mdrokz/42f8741b7f369adf6ff4a487caeb97fd to your computer and use it in GitHub Desktop.
Leetcode problem 1603 design a parking system implemented in rust
enum CarType {
Big(i32),
Med(i32),
Small(i32)
}
struct ParkingSystem {
big: CarType,
medium: CarType,
small: CarType
}
impl CarType {
fn get_count(&self) -> i32 {
match self {
CarType::Big(count) => *count,
CarType::Med(count) => *count,
CarType::Small(count) => *count
}
}
fn remove_car(&mut self) {
match self {
CarType::Big(count) => *count -= 1,
CarType::Med(count) => *count -= 1,
CarType::Small(count) => *count -= 1
}
}
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl ParkingSystem {
fn new(big: i32, medium: i32, small: i32) -> Self {
Self {
big: CarType::Big(big),
medium: CarType::Med(medium),
small: CarType::Small(small)
}
}
fn add_car(&mut self, car_type: i32) -> bool {
match car_type {
1 => {
if self.big.get_count() > 0 {
self.big.remove_car();
return true;
}
return false;
},
2 => {
if self.medium.get_count() > 0 {
self.medium.remove_car();
return true;
}
return false;
},
3 => {
if self.small.get_count() > 0 {
self.small.remove_car();
return true;
}
return false;
},
_ => {
return false;
}
}
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* let obj = ParkingSystem::new(big, medium, small);
* let ret_1: bool = obj.add_car(carType);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment