Get Minimum Amount
Learn this problemProblem statement
The manager of the Amazon warehouse has decided to make changes to the inventory. Currently, the inventory has n products, where the quality of the ith product after quality checks is represented by the array element quality[i].
The manager wants to create an optimal inventory, where the array of products quality follows the following property:
- All occurrences of each quality value must be contiguous.
In order to convert the inventory into an optimal inventory, the manager can do the following operation any number of times:
- Choose two quality values
xandy. - Replace every product with quality
xto have qualityyinstead. - This operation costs
num_replacementsunits of money, wherenum_replacementsis the number of products whose quality was changed.
Given n products and an array quality, find the minimum amount of money the manager has to spend to convert the inventory into an optimal inventory.
Note: The quality of a product can be negative indicating that the product is of poor quality.
Function
getMinAmount(quality: int[]) → intComplete the function getMinAmount in the editor below.
getMinAmount has the following parameter(s):
int quality[n]: the quality of products
Returns
int: the minimum amount of money the manager has to spend to convert the inventory into an optimal inventory.
Examples
Example 1
quality = [7, 7, 5, 7, 3, 5, 3]return = 4
Given n = 7, quality = [7, 7, 5, 7, 3, 5, 3].
One of the optimal ways to convert is explained below:
Hence, the total amount spent is 4.
More Amazon problems
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026
- First Valid Word SegmentationONSITE INTERVIEW · Seen Jul 2026
- Nearby Fulfillment Centers with InventoryONSITE INTERVIEW · Seen Jul 2026