Problem · Array
Costliest Chocolate
Learn this problemProblem statement
A chocolate catalog is represented offline by aligned arrays:
recordBrands[i]is the brand of chocolatei.productNumbers[i]is its unique product identifier.prices[i][j]andweights[i][j]are the price and weight in grams of variationjof that chocolate.
Among chocolates whose brand exactly equals brand, evaluate the price-per-gram ratio prices[i][j] / weights[i][j] for every variation. Return the productNumber of the chocolate containing the largest ratio.
Compare ratios as exact fractions. If multiple chocolates share the largest ratio, return the smaller productNumber.
Function
costliestChocolate(brand: String, recordBrands: String[], productNumbers: long[], prices: int[][], weights: int[][]) → longExamples
Example 1
brand = "ABC"recordBrands = ["ABC","ABC","Other"]productNumbers = [20,10,5]prices = [[400,900],[500],[1000]]weights = [[100,200],[100],[10]]return = 10For product 20, the best ratio is 900 / 200 = 4.5. Product 10 has ratio 500 / 100 = 5, which is larger. Product 5 belongs to another brand and is ignored.
Example 2
brand = "Cocoa"recordBrands = ["Cocoa","Cocoa","Other"]productNumbers = [42,7,1]prices = [[6],[9],[1000]]weights = [[2],[3],[1]]return = 7Products 42 and 7 both have price-per-gram ratio 3. The tie is resolved by the smaller product number, so the answer is 7. The higher ratio from Other is irrelevant.
Constraints
1 <= recordBrands.length == productNumbers.length == prices.length == weights.length <= 10^51 <= sum(prices[i].length) <= 2 * 10^5prices[i].length == weights[i].length1 <= prices[i].length1 <= prices[i][j], weights[i][j] <= 10^91 <= productNumbers[i] <= 10^18- All product numbers are unique.
- At least one entry has
recordBrands[i] == brand. - Brand comparison is case-sensitive.