Dynamic Restaurant Ratings
At Zomato, real-time insights into restaurant performance, especially customer ratings, are crucial for operational decisions, marketing strategies, and improving customer satisfaction. This problem simulates a core system responsible for tracking and analyzing restaurant ratings dynamically. You will implement a data structure that efficiently handles updates to individual restaurant ratings and queries for the average rating across a range of restaurants.
Your task is to implement the RestaurantRatings class with the following methods:
constructor(initialRatings: number[]):
i-th element in initialRatings corresponds to the rating of the restaurant at index i.updateRating(restaurantIndex: number, newRating: number): void:
restaurantIndex to newRating.getAverageRating(startIndex: number, endIndex: number): number:
[startIndex, endIndex].constructor(initialRatings: number[])
initialRatings: An array of number representing the initial ratings of restaurants. Each rating is a floating-point value.void (constructor returns null in test cases).updateRating(restaurantIndex: number, newRating: number): void
restaurantIndex: An number representing the 0-based index of the restaurant whose rating is to be updated.newRating: A number representing the new rating for the specified restaurant.void.getAverageRating(startIndex: number, endIndex: number): number
startIndex: An number representing the 0-based starting index of the range (inclusive).endIndex: An number representing the 0-based ending index of the range (inclusive).number (floating-point) representing the average rating of restaurants in the given range. The result should be accurate to at least 5 decimal places.startIndex and endIndex for getAverageRating are inclusive.startIndex <= endIndex for all queries.Examples
Example 1
Input:
operations = ["RestaurantRatings","getAverageRating","updateRating","getAverageRating","getAverageRating"], params = [[[3.5, 4.0, 2.5, 5.0]], [0, 3], [1, 4.5], [0, 1], [2, 3]]
Output:
[null,3.75,null,4.0,3.75]
Note:
[3.5, 4.0, 2.5, 5.0].[0, 3]: (3.5 + 4.0 + 2.5 + 5.0) / 4 = 15.0 / 4 = 3.75.[3.5, 4.5, 2.5, 5.0].[0, 1]: (3.5 + 4.5) / 2 = 8.0 / 2 = 4.0.[2, 3]: (2.5 + 5.0) / 2 = 7.5 / 2 = 3.75.Example 2
Input:
operations = ["RestaurantRatings","getAverageRating","updateRating","getAverageRating"], params = [[[1.0]], [0, 0], [0, 5.0], [0, 0]]
Output:
[null,1.0,null,5.0]
Note:
[1.0].[0, 0]: 1.0 / 1 = 1.0.[5.0].[0, 0]: 5.0 / 1 = 5.0.Constraints
1 <= initialRatings.length <= 10^50.0 <= initialRatings[i] <= 5.00 <= restaurantIndex < initialRatings.length0.0 <= newRating <= 5.00 <= startIndex <= endIndex < initialRatings.lengthupdateRating and getAverageRating will not exceed 10^5.updateRating and getAverageRating run in O(log N) time complexity, and the constructor in O(N).Loading editor...