Problem

Get Minimum Amount

Learn this problem
AmazonNEW GRADOA
See Amazon hiring insights

Problem 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:

  1. Choose two quality values x and y.
  2. Replace every product with quality x to have quality y instead.
  3. This operation costs num_replacements units of money, where num_replacements is 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[]) → int

Complete 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
Example 1 illustration

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

drafts saved locally
public int getMinAmount(int[] quality) {
  // write your code here
}
quality[7, 7, 5, 7, 3, 5, 3]
expected4
checking account