Problem · Array

Costliest Chocolate

Learn this problem
EasyRippling logoRipplingNEW GRADINTERNOA

Problem statement

A chocolate catalog is represented offline by aligned arrays:

  • recordBrands[i] is the brand of chocolate i.
  • productNumbers[i] is its unique product identifier.
  • prices[i][j] and weights[i][j] are the price and weight in grams of variation j of 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[][]) → long

Examples

Example 1

brand = "ABC"recordBrands = ["ABC","ABC","Other"]productNumbers = [20,10,5]prices = [[400,900],[500],[1000]]weights = [[100,200],[100],[10]]return = 10

For 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 = 7

Products 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^5
  • 1 <= sum(prices[i].length) <= 2 * 10^5
  • prices[i].length == weights[i].length
  • 1 <= prices[i].length
  • 1 <= prices[i][j], weights[i][j] <= 10^9
  • 1 <= productNumbers[i] <= 10^18
  • All product numbers are unique.
  • At least one entry has recordBrands[i] == brand.
  • Brand comparison is case-sensitive.

More Rippling problems

drafts saved locally
public long costliestChocolate(String brand, String[] recordBrands, long[] productNumbers, int[][] prices, int[][] weights) {
  // Write your code here.
}
brand"ABC"
recordBrands["ABC","ABC","Other"]
productNumbers[20,10,5]
prices[[400,900],[500],[1000]]
weights[[100,200],[100],[10]]
expected10
checking account