Skip to content

Commit 664c4f6

Browse files
committed
https://leetcode.cn/problems/design-a-food-rating-system/
1 parent 9b5fb55 commit 664c4f6

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/design-a-food-rating-system/
14+
1315
https://leetcode.cn/problems/minimum-score-after-removals-on-a-tree/
1416

1517
https://leetcode.cn/problems/first-common-ancestor-lcci/

design-a-food-rating-system/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { AvlTree } from "https://esm.sh/@datastructures-js/[email protected]/?dts";
2+
3+
class FoodRatings {
4+
#cuisineToTree = new Map<string, AvlTree<string>>();
5+
#foodToTree = new Map<string, AvlTree<string>>();
6+
#foodToRating = new Map<string, number>();
7+
constructor(foods: string[], cuisines: string[], ratings: number[]) {
8+
for (const [index, food] of foods.entries()) {
9+
const cuisine = cuisines[index];
10+
const rating = ratings[index];
11+
this.#foodToRating.set(food, rating);
12+
const tree = this.#cuisineToTree.get(cuisine) ??
13+
new AvlTree((a, b) => {
14+
const ra = this.#foodToRating.get(a) ?? 0;
15+
const rb = this.#foodToRating.get(b) ?? 0;
16+
return ra === rb ? a.localeCompare(b) : -ra + rb;
17+
});
18+
19+
this.#cuisineToTree.set(cuisine, tree);
20+
this.#foodToTree.set(food, tree);
21+
22+
tree.insert(food);
23+
}
24+
}
25+
26+
changeRating(food: string, newRating: number): void {
27+
const tree = this.#foodToTree.get(food);
28+
if (!tree) throw Error("not found");
29+
30+
tree.remove(food);
31+
this.#foodToRating.set(food, newRating);
32+
tree.insert(food);
33+
}
34+
35+
highestRated(cuisine: string): string {
36+
return this.#cuisineToTree.get(cuisine)?.min()?.getValue() ?? "";
37+
}
38+
}
39+
export default FoodRatings;

last-stone-weight/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PriorityQueue } from "../kth-largest-element-in-a-stream/PriorityQueue.ts";
22

33
export default function lastStoneWeight(stones: number[]): number {
4-
const maxheap = PriorityQueue<number>((a, b) =>- a + b);
4+
const maxheap = PriorityQueue<number>((a, b) => -a + b);
55
stones.forEach((stone) => maxheap.offer(stone));
66
while (maxheap.length() >= 2) {
77
const y = maxheap.shift();

0 commit comments

Comments
 (0)