FastPrepMoving Cost by Volume and Category
Problem · Array

Moving Cost by Volume and Category

Learn this problem
EasyWayfair logoWayfairFULLTIMEONSITE INTERVIEW

Problem statement

You are estimating one move. Each row packages[i] = [category, length, width, height] describes one rectangular package. Category IDs are zero-based indices into categoryRates.

categoryRates[c] is the cost charged for moving one cubic unit of category c. A package's volume is length × width × height, and its moving cost is volume × categoryRates[category].

Return the total moving cost of all packages. Use 64-bit arithmetic for every product and the accumulated total.

Function

calculateMovingCost(packages: int[][], categoryRates: int[]) → long

Examples

Example 1

packages = [[0,2,3,4],[1,1,2,5]]categoryRates = [3,5]return = 122

The package volumes are 24 and 10, so the total cost is 24 × 3 + 10 × 5 = 122.

Example 2

packages = [[2,3,3,3],[0,10,1,1],[2,1,1,1]]categoryRates = [2,7,4]return = 132

Category 2 contributes 28 cubic units at rate 4 and category 0 contributes 10 at rate 2, for total cost 28 × 4 + 10 × 2 = 132.

Example 3

packages = [[1,100,100,100],[1,100,100,100],[1,100,100,100],[1,100,100,100]]categoryRates = [1,1000]return = 4000000000

Each package has volume 1,000,000 and costs 1,000,000,000. Four packages cost 4,000,000,000, which requires 64-bit accumulation.

Constraints

  • 1 <= packages.length <= 10000.
  • Every row in packages contains exactly four integers [category, length, width, height].
  • 1 <= categoryRates.length <= 100, and 0 <= category < categoryRates.length.
  • 1 <= length, width, height <= 100.
  • 1 <= categoryRates[c] <= 1000.
  • The returned total fits in a signed 64-bit integer.

More Wayfair problems

drafts saved locally
public long calculateMovingCost(int[][] packages, int[] categoryRates) {
    // Write your code here.
}
packages[[0,2,3,4],[1,1,2,5]]
categoryRates[3,5]
expected122
checking account