Suppose we have a list called reviews and a threshold value t. Each item in reviews[i] has [x, y] means product i had x number of 5-star rating and y number of reviews. We have to find the minimum number of additional 5-star reviews we need so that the percentage of 5-star reviews for those items list is at least t percent.
So, if the input is like reviews = [[3, 4],[1, 2],[4, 6]] threshold = 78, then the output will be 7, as in total there were 8 5-star reviews and 12 reviews. To reach 78% 5-star reviews, we need 7 more 5-star reviews.
To solve this, we will follow these steps −
a := 0, b := 0
for each 5-star count c and review count d in reviews, do
a := a + c
b := b + d
if a * 100 >= t * b, then
return 0
delta := t * b - 100 * a
return floor of (delta +(99 - t))/(100 - t)
Example
Let us see the following implementation to get better understanding
def solve(reviews, t):
a = 0
b = 0
for c, d in reviews:
a += c
b += d
if a * 100 >= t * b:
return 0
delta = t * b - 100 * a
return (delta + (99 - t)) // (100 - t)
reviews = [
[3, 4],
[1, 2],
[4, 6]
]
t = 78
print(solve(reviews, t))Input
[[3, 4], [1, 2],[4, 6] ],78
Output
7