184k views
3 votes
2. Retail Inventory Management Alice went to buy products from an inventory in a retail mart. Each inventory has various products, all with varying weights. Alice decides to use a scooper that can pick up three products at a time. The products in each inventory are lined up in a single row, and Alice indexes them from 0 to n−1, starting from the first product to the n

th
product in the row. In each selection, Alice picks the lightest remaining product in the inventory with weight w and uses the scooper to pick up that product along with the two other products adjacent to it. Alice repeats this process until there are no more products left in the inventory. Alice wants to find the sum of the weights of the lightest products which can be chosen in every selection. Note: If there are two products with the lightest weight at different indexes, Alice chooses the product at the smallest index. If the product only has one other product adjacent to it, then the product itself and the single adjacent product will be removed. Example Let there be n=4 products in the inventory with weights represented by weights =[4,3,2,1]. - First, choose the minimum weight (i.e., 1) and add that weight up to the total. The products with weights 2 and 1 are removed. The array of products is now [4,3]. - Then, choose the minimum weight from the remaining products (i.e., 3) and add that weight up to the total. The products with weights 3 and 4 are removed, and now there are no more products in the inventory. Hence, the total is 1+3=4. Function Description Complete the function findTotalWeight in the editor below. findTotalWeight has the following parameter: products: the array of integers denoting the weights of the products in the inventory Returns int: an integer denoting the sum of minimum weighted products at each selection. Constraints - 3≤w≤2000 - 3≤ length of products ≤2000 - 1≤ products [i]≤10
5
Language Python 3 Q Autocol

1 Answer

2 votes

Final answer:

To solve this problem, iterate through the products array and keep track of the minimum weight encountered so far. At each iteration, add the minimum weight to the total sum and update the minimum weight if a lighter product is found. Repeat until the end of the array. The final sum is the answer.

Step-by-step explanation:

In this problem, Alice is selecting three adjacent products at a time, starting with the lightest product and removing it along with the two adjacent products. The goal is to find the sum of the weights of the lightest products chosen at each selection. To solve this problem, we can iterate through the products array and keep track of the minimum weight encountered so far. At each iteration, we add the minimum weight to the total sum and update the minimum weight if we encounter a lighter product. We repeat this process until we reach the end of the array. The final sum will be the answer.

For example, given an array of products with weights [4,3,2,1], we start by choosing the product with weight 1. The array becomes [4,3]. Then, we choose the product with weight 3. The array becomes empty. The sum is 1+3 = 4.

User Abhinav Risal
by
8.7k points