feat: 2353_design_a_food_rating_system
This commit is contained in:
parent
d5c099d9b3
commit
fd70e0907c
7
2353_design_a_food_rating_system/Cargo.lock
generated
Normal file
7
2353_design_a_food_rating_system/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "design_a_food_rating_system"
|
||||
version = "0.1.0"
|
6
2353_design_a_food_rating_system/Cargo.toml
Normal file
6
2353_design_a_food_rating_system/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "design_a_food_rating_system"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
93
2353_design_a_food_rating_system/src/lib.rs
Normal file
93
2353_design_a_food_rating_system/src/lib.rs
Normal file
@ -0,0 +1,93 @@
|
||||
use std::collections::{BinaryHeap, HashMap};
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
struct Cuisine {
|
||||
name: String,
|
||||
style: String,
|
||||
rating: i32,
|
||||
}
|
||||
|
||||
impl Ord for Cuisine {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.rating
|
||||
.cmp(&other.rating)
|
||||
.then(other.name.cmp(&self.name))
|
||||
.then(self.style.cmp(&other.style))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Cuisine {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FoodRatings {
|
||||
cuisines: HashMap<String, Cuisine>,
|
||||
max_heaps: HashMap<String, BinaryHeap<Cuisine>>,
|
||||
}
|
||||
|
||||
impl FoodRatings {
|
||||
pub fn new(foods: Vec<String>, styles: Vec<String>, ratings: Vec<i32>) -> Self {
|
||||
let cuisines_iter =
|
||||
foods
|
||||
.iter()
|
||||
.zip(styles.iter())
|
||||
.zip(ratings.iter())
|
||||
.map(|((name, style), rating)| Cuisine {
|
||||
name: name.clone(),
|
||||
style: style.clone(),
|
||||
rating: *rating,
|
||||
});
|
||||
|
||||
let mut max_heaps: HashMap<String, BinaryHeap<Cuisine>> = HashMap::new();
|
||||
for cuisine in cuisines_iter.clone() {
|
||||
if let Some(heap) = max_heaps.get_mut(&cuisine.style) {
|
||||
heap.push(cuisine);
|
||||
} else {
|
||||
let mut heap = BinaryHeap::new();
|
||||
heap.push(cuisine.clone());
|
||||
max_heaps.insert(cuisine.style, heap);
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
cuisines: HashMap::from_iter(
|
||||
cuisines_iter.map(|cuisine| (cuisine.name.clone(), cuisine)),
|
||||
),
|
||||
max_heaps: max_heaps,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn change_rating(&mut self, food_name: String, new_rating: i32) {
|
||||
if let Some(cuisine) = self.cuisines.get_mut(&food_name) {
|
||||
cuisine.rating = new_rating;
|
||||
|
||||
if let Some(heap) = self.max_heaps.get_mut(&cuisine.style) {
|
||||
heap.push(cuisine.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn highest_rated(&mut self, style: String) -> String {
|
||||
let mut result = String::new();
|
||||
let heap = self.max_heaps.get_mut(&style).unwrap();
|
||||
|
||||
while let Some(highest) = heap.peek() {
|
||||
if highest.rating
|
||||
!= self
|
||||
.cuisines
|
||||
.get(&highest.name)
|
||||
.map(|cuisine| cuisine.rating)
|
||||
.unwrap_or_default()
|
||||
{
|
||||
heap.pop();
|
||||
} else {
|
||||
result = highest.name.clone();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user