Optimal Warehouse Picking Path
In a large-scale logistics operation like Delhivery, optimizing warehouse processes is crucial for efficiency. One common challenge is picking items from densely packed storage racks where items are stacked vertically. To pick an item, all items physically located above it in the same stack must be removed first. This problem focuses on finding the most cost-effective sequence to pick a specific set of target items, considering the 'effort' (total weight) involved in moving all necessary items.
Given a representation of multiple item stacks in a warehouse, a list of specific items that must be picked, and the individual weights of all items, your task is to implement a function minPickingCost that calculates the minimum total weight of all items that need to be picked to successfully retrieve all specified target items. This includes items that are not targets themselves but must be picked to access a target item.
Your function should adhere to the following signature:
function minPickingCost(
stacks: number[][],
targetItems: number[],
itemWeights: Record<number, number>
): number
stacks: A number[][] where stacks[i] represents the i-th stack of items. Each inner array stacks[i] contains item IDs, ordered from top to bottom. stacks[i][0] is the topmost item, stacks[i][1] is the item below it, and so on.targetItems: A number[] containing the unique IDs of items that must be picked. The order of items in this array does not imply any picking order.itemWeights: A Record<number, number> (or plain JavaScript object) mapping item_id (number) to its weight (number).number representing the minimum total weight of all items (target or non-target) that are picked to satisfy the targetItems requirement.targetItems: If targetItems is empty, no items need to be picked, so the cost is 0.targetItems are guaranteed to exist within the stacks.Examples
Example 1
Input:
stacks = [[101, 102], [201]], targetItems = [102, 201], itemWeights = {"101": 5, "102": 10, "201": 7}
Output:
22
Note:
To get item 102, item 101 (weight 5) must be picked first. To get item 201, only item 201 (weight 7) needs to be picked. The optimal path involves picking all items. Total cost = 5 (for 101) + 10 (for 102) + 7 (for 201) = 22.
Example 2
Input:
stacks = [[1, 2], [3]], targetItems = [2, 3], itemWeights = {"1": 100, "2": 10, "3": 5}
Output:
115
Note:
To pick target item 2, item 1 (weight 100) must be picked first. Then pick 2 (weight 10). Separately, pick target item 3 (weight 5). The order of picking 1/2 vs 3 doesn't change the total cost as both items 1 and 2 must be picked to get 2. Total cost = 100 + 10 + 5 = 115.
Example 3
Input:
stacks = [[10, 20, 30], [40, 50]], targetItems = [30, 50], itemWeights = {"10": 1, "20": 1, "30": 100, "40": 5, "50": 10}
Output:
117
Note:
To get target item 30, items 10 (weight 1) and 20 (weight 1) must be picked first. Then item 30 (weight 100). To get target item 50, item 40 (weight 5) must be picked first. Then item 50 (weight 10). Regardless of the picking order, all items (10, 20, 30, 40, 50) must eventually be picked. Total cost = 1 + 1 + 100 + 5 + 10 = 117.
Constraints
1 <= stacks.length <= 5 (Number of stacks, denoted as N)1 <= stacks[i].length <= 5 for each stack i (Maximum height of a stack, denoted as M)0 <= targetItems.length <= 10 (Number of target items, denoted as K)1 <= item_id <= 10001 <= weight <= 1000 for all item weights.N * M.Loading editor...