deno.land / x / masx200_leetcode_test@10.6.5 / design-a-food-rating-system / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { AvlTree } from "https://esm.sh/@datastructures-js/binary-search-tree@5.0.2/?dts";
class FoodRatings { #cuisineToTree = new Map<string, AvlTree<string>>(); #foodToTree = new Map<string, AvlTree<string>>(); #foodToRating = new Map<string, number>(); 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;
masx200_leetcode_test

Version Info

Tagged at
a year ago