32.1k views
0 votes
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 pilt 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ᵗʰ 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 Note: If there are two products with the lightest weight at different indexes, Alice chooses the produgt 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⁵ Sample Output Explanation First, select the minimum weight, 4 . Its adjacent products have weights 6 and 9 . Weights 6,9 , and 4 will be removed from products. The new minimum weight is 10 .

User Chriamue
by
8.3k points

1 Answer

5 votes

Final answer:

To find the sum of the weights of the lightest products at each selection in Alice's scooper, we use a sliding window technique to track the minimum weight and update the window.

Step-by-step explanation:

In this problem, Alice is buying products from an inventory in a retail mart. The products have varying weights and are lined up in a single row. Alice uses a scooper to pick up three products at a time, starting with the lightest remaining product. She repeats this process until there are no more products left. The goal is to find the sum of the weights of the lightest products at each selection.

The approach to solve this problem is to use a sliding window technique. We keep track of the minimum weight in the current window and add it to the total weight. Then, we update the window by removing the first element and adding the next element from the inventory. We continue this process until we have iterated through all the products in the inventory.

User Thedjaney
by
8.2k points