Item Prices From Material Costs
Updated
Problem statement
A workshop buys materials by the unit. Material materials[j] costs prices[j] per unit.
There are m items to price. Item i needs itemQuantities[i][k] units of material itemMaterials[i][k], for every k. The price of an item is the total cost of the materials it needs: the sum of amount times unit price.
Return an array answer of length m, where answer[i] is the price of item i.
- If item
ineeds a material that does not appear inmaterials, its price cannot be worked out, andanswer[i]is-1. - An item that needs no materials costs
0.
Function
itemPrices(materials: String[], prices: int[], itemMaterials: String[][], itemQuantities: int[][]) → long[]Examples
Example 1
materials = ["wood","nail","glue"]prices = [5,1,3]itemMaterials = [["wood","nail"],["glue","wood"],["nail"]]itemQuantities = [[2,10],[1,4],[25]]return = [20,23,25]Item 0 needs 2 wood and 10 nails: 2 * 5 + 10 * 1 = 20. Item 1 needs 1 glue and 4 wood: 1 * 3 + 4 * 5 = 23. Item 2 needs 25 nails: 25 * 1 = 25.
Example 2
materials = ["steel","rubber"]prices = [40,7]itemMaterials = [["steel","paint"],["rubber"],[]]itemQuantities = [[1,2],[4],[]]return = [-1,28,0]Item 0 needs paint, which has no price in the list, so its price is -1. Item 1 costs 4 * 7 = 28. Item 2 needs nothing, so it costs 0.
Example 3
materials = ["gold","silk"]prices = [1000000,250]itemMaterials = [["silk","gold"]]itemQuantities = [[10000,10000]]return = [10002500000]The single item costs 10000 * 250 + 10000 * 1000000 = 10002500000. That is larger than the largest 32-bit integer, 2147483647, so the sum needs a 64-bit integer.
Constraints
1 <= materials.length <= 1000prices.length == materials.length1 <= prices[j] <= 10^61 <= itemMaterials.length <= 500itemQuantities.length == itemMaterials.length0 <= itemMaterials[i].length <= 50, anditemQuantities[i].length == itemMaterials[i].length1 <= itemQuantities[i][k] <= 10^4- Every name in
materialsanditemMaterialshas 1 to 10 lowercase English letters. - The names in
materialsare all different, and the names in eachitemMaterials[i]are all different.
More C3.ai interview questions
- Fewest Turns to Open a Wheel LockPHONE SCREEN · Seen Nov 2024
- Days Until a Warmer DayPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Longest Run of Ones After Flipping k ZerosPHONE SCREEN · Seen Mar 2022
- Top K Frequent ElementsPHONE SCREEN · Seen Feb 2021
- Adjacent-Key Lock PatternsPHONE SCREEN · Seen Feb 2014
- Drop Recent Duplicate MessagesOA · Seen Oct 2026
- Maximum K-Star SumOA · Seen Oct 2026
- Every Result of Grouping an ExpressionONSITE INTERVIEW · Seen Jan 2023
- Fewest Jumps to the Last IndexONSITE INTERVIEW · Seen Jan 2023
- Rotate a Square Grid ClockwiseONSITE INTERVIEW · Seen Jan 2023
Similar questions at other companies
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Most Frequent NameAmazon · PHONE SCREEN
- Words with Repeated AnagramsThe Voleon Group · PHONE SCREEN
- Project JSON Fields into a Target FormatBooking.com · PHONE SCREEN
- Product Keyword SearchTesco · PHONE SCREEN
- Verify Words Sorted by a Custom AlphabetTrueFoundry · PHONE SCREEN
- Character Frequencies Across Nested String ListsSnowflake · PHONE SCREENSnowflake interview questions