5-Star Review Percentage - Google Top Interview Questions


Problem Statement :


You are given a two-dimensional list of integers reviews and a positive integer threshold. Each element reviews[i] contains [x, y] meaning product i had x number of 5-star reviews and a total of y reviews. All reviews are for one store.

Return the minimum number of additional 5-star reviews we need such that the percentage of 5-star reviews in the store is at least threshold. You can assume that it's possible to reach threshold% of 5-star reviews.

Constraints

1 ≤ n ≤ 100,000 where n is the length of reviews

0 ≤ threshold ≤ 100

Example 1

Input

reviews = [
    [4, 4],
    [1, 2],
    [3, 6]
]

threshold = 77

Output

6

Explanation

So in total there were 8 5-star reviews and a total of 12 reviews. To reach 77% 5-star reviews, we need 6 
more 5-star reviews.

Example 2

Input
reviews = [
    [10, 20]
]

threshold = 50

Output

0

Explanation

We're already at 50% 5-star reviews.

Example 3

Input

reviews = [
    [1, 1]
]

threshold = 100

Output

0

Explanation

We're already at 100% 5-star reviews.



Solution :



                        Solution in C++ :

int solve(vector<vector<int>>& reviews, int threshold) {
    double fi = 0, xi = 0;
    for (int i = 0; i < reviews.size(); i++) {
        fi += reviews[i][1];
        xi += reviews[i][0];
    }

    return max((double)0,
               ceil((threshold * fi - 100 * xi) / max((double)1, (100 - (double)threshold))));
}
                    

                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(int[][] reviews, int threshold) {
        if (threshold == 0)
            return 0;
        int totalReview = 0;
        int total5Star = 0;
        for (int i = 0; i < reviews.length; i++) {
            totalReview += reviews[i][1];
            total5Star += reviews[i][0];
        }

        // (totalReview + x) * threshold / 100 = (total5Star + x)

        int res = (int) Math.ceil(
            (double) ((totalReview * threshold) - (100 * total5Star)) / (100 - threshold));

        return res;
    }
}
                    

                        Solution in Python : 
                            
class Solution:
    def solve(self, reviews, threshold):
        a = 0
        b = 0
        for c, d in reviews:
            a += c
            b += d
        if a * 100 >= threshold * b:
            return 0
        delta = threshold * b - 100 * a
        return (delta + (99 - threshold)) // (100 - threshold)
                    

View More Similar Problems

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →