The manager of an Amazon warehouse wants to change the inventory so it becomes optimal. There are n products, and the quality of the i-th product after quality checks is represented by quality[i].
An inventory is optimal if all occurrences of each quality value are contiguous. In other words, after the changes, no quality value should appear in two separated groups.
The manager may perform the following operation any number of times:
- Choose two quality values
xandy. - Replace every product with quality
xby qualityy. - The operation costs
num_replacements, wherenum_replacementsis the number of products whose quality was changed.
Given the array quality, return the minimum amount of money needed to convert the inventory into an optimal inventory.
Note: Product quality can be negative, indicating that the product is of poor quality.
Complete the function getMinAmount.
getMinAmount has the following parameter:
int quality[n]: the quality values of the products
Returns
int: the minimum amount of money needed to convert the inventory into an optimal inventory.
quality = [7, 7, 5, 7, 3, 5, 3] return = 4
One optimal way is to replace all products with quality 3 by 7, costing 2, and then replace all products with quality 5 by 7, costing another 2. The final inventory contains only quality 7, so it is optimal. The total cost is 4.
quality = [1, 2, 1] return = 1
Replace the single product with quality 2 by quality 1. The resulting inventory is [1, 1, 1], and the cost is 1.
1 <= n <= 2 * 10^5-10^9 <= quality[i] <= 10^9
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
- Product Category Group SizesPHONE SCREEN · Seen May 2026
- Count Connected ComponentsPHONE SCREEN · Seen May 2026
- Drone Delivery RouteOA · Seen May 2026
public int getMinAmount(int[] quality) {
// write your code here
}