Skip to content

Commit 64448e6

Browse files
authored
Create 2353-design-a-food-rating-system.kt
1 parent ae22763 commit 64448e6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class FoodRatings(foods: Array<String>, cuisines: Array<String>, ratings: IntArray) {
2+
3+
val ratingsList = HashMap<String, SortedSet<Pair<Int, String>>>()
4+
val cuisinesList = HashMap<String, String>()
5+
val foodRatingsList = HashMap<String, Int>()
6+
7+
init {
8+
for (i in foods.indices) {
9+
ratingsList.getOrPut(cuisines[i]) {
10+
sortedSetOf(compareBy({ -it.first }, { it.second }))
11+
}.apply {
12+
add(ratings[i] to foods[i])
13+
}
14+
cuisinesList[foods[i]] = cuisines[i]
15+
foodRatingsList[foods[i]] = ratings[i]
16+
}
17+
}
18+
19+
fun changeRating(food: String, newRating: Int) {
20+
val cuisine = cuisinesList[food] ?: ""
21+
val rating = foodRatingsList[food] ?: 1
22+
23+
ratingsList[cuisine]?.let {
24+
it.remove(rating to food)
25+
it.add(newRating to food)
26+
}
27+
28+
foodRatingsList[food] = newRating
29+
}
30+
31+
fun highestRated(cuisine: String): String {
32+
return ratingsList[cuisine]?.first()?.second ?: ""
33+
}
34+
35+
}

0 commit comments

Comments
 (0)