import { AvlTree } from "npm:@datastructures-js/binary-search-tree@5.3.1"; class FoodRatings { #cuisineToTree = new Map>(); #foodToTree = new Map>(); #foodToRating = new Map(); constructor(foods: string[], cuisines: string[], ratings: number[]) { for (const [index, food] of foods.entries()) { const cuisine = cuisines[index]; const rating = ratings[index]; this.#foodToRating.set(food, rating); const tree = this.#cuisineToTree.get(cuisine) ?? new AvlTree((a, b) => { const ra = this.#foodToRating.get(a) ?? 0; const rb = this.#foodToRating.get(b) ?? 0; return ra === rb ? a.localeCompare(b) : -ra + rb; }); this.#cuisineToTree.set(cuisine, tree); this.#foodToTree.set(food, tree); tree.insert(food); } } changeRating(food: string, newRating: number): void { const tree = this.#foodToTree.get(food); if (!tree) throw Error("not found"); tree.remove(food); this.#foodToRating.set(food, newRating); tree.insert(food); } highestRated(cuisine: string): string { return this.#cuisineToTree.get(cuisine)?.min()?.getValue() ?? ""; } } export default FoodRatings;