Amazon OA — Minimum Cost to Make Inventory Optimal – 一亩三分地

📝 Problem Description (Original)

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 i-th product after quality check is represented by an array quality[i].

The manager wants to create an optimal inventory, where:

All occurrences of each quality value must be contiguous.


⚙️ Operation

To convert the inventory into an optimal one, the manager can perform 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. The cost of this operation is: num_replacements = number of products whose quality was changed

🎯 Goal

Given the array quality, return:

The minimum total cost required to transform the inventory into an optimal inventory.


⚠️ Notes

  • quality[i] can be negative
  • Same value must appear in one continuous segment

📥 Function Signature

int getMinAmount(List<Integer> quality)

📊 Constraints

1 ≤ n ≤ 2 * 10^5
-10^9 ≤ quality[i] ≤ 10^9

📌 Example 1

Input

n = 5
quality = [1, 2, 1, 2, 1]

Output

2

Explanation

Replace all 2 → 1

Result:

[1, 1, 1, 1, 1]

Cost = 2


📌 Example 2

Input

n = 11
quality = [10, 6, 10, -3, 1, 1, 4, -4, -1, 1, -7]

Output

4

Explanation

Operation 1: 6 → 10
Operation 2: 4 → 1
Operation 3: -4 → 1
Operation 4: -1 → 1

我们也有代面试,面试辅助,OA代写等服务助您早日上岸~

Leave a Reply

Your email address will not be published. Required fields are marked *