Minimize Replacements So Equal Product Values Are Contiguous
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
# Minimize Replacements So Equal Product Values Are Contiguous
You are given the quality value of each product in a line. In one operation, choose two distinct values `x` and `y` and replace every occurrence of `x` with `y`. The cost of the operation is the number of elements replaced.
Find the minimum total cost needed to make every value that remains in the array occupy exactly one contiguous block.
```python
def min_contiguous_replacement_cost(quality: list[int]) -> int:
...
```
You may perform any number of operations, and a replacement can merge a value into another value that already exists. Return only the minimum cost; you do not need to return the operations.
## Constraints
- `0 <= len(quality) <= 200_000`
- `-10^9 <= quality[i] <= 10^9`
- The answer fits in a signed 64-bit integer.
## Examples
```text
Input: quality = [1, 2, 1, 3, 3]
Output: 1
Explanation: Replacing the single 2 with 1 makes each remaining value contiguous.
```
```text
Input: quality = [1, 2, 1, 2]
Output: 2
```
```text
Input: quality = [4, 4, 7, 7, 9]
Output: 0
```
Quick Answer: Minimize the replacement cost needed so every remaining product value occupies one contiguous block, where one operation changes all occurrences of one value into another. The challenge tests compression of repeated runs, global merge effects, weighted decisions, and scalable reasoning for arrays with up to 200,000 entries.
You are given `quality`, a list of integers in which `quality[i]` is the quality value of the `i`-th product standing in a line.
In one operation you choose two distinct values `x` and `y` and replace every occurrence of `x` with `y`. The cost of that operation is the number of elements it rewrites. You may perform any number of operations, and a replacement may merge a value into another value that already exists.
Return the minimum total cost needed to reach a line in which every value that still appears occupies exactly one contiguous block: for each remaining value `v`, the set of indices holding `v` must be an unbroken range.
Return only the minimum total cost; you do not need to return the operations.
## Output semantics
The function returns a single integer, so exactly one answer is correct for any input — there is no ordering, tie-breaking, or formatting ambiguity. An empty line already satisfies the condition and costs `0`. The cost is never negative and never exceeds `len(quality)`.
## Constraints
- `0 <= len(quality) <= 200000`
- `-10^9 <= quality[i] <= 10^9`
- The returned cost satisfies `0 <= cost <= len(quality) <= 200000` and fits in a signed 64-bit integer.
## Examples
**Example 1**
```text
Input: quality = [1, 2, 1, 3, 3]
Output: 1
```
Value `1` sits at indices 0 and 2, so the single `2` at index 1 splits it. Replacing that `2` with `1` costs 1 and yields `[1, 1, 1, 3, 3]`, where `1` occupies indices 0..2 and `3` occupies indices 3..4. No sequence of operations costs less, because at least one element has to be rewritten.
**Example 2**
```text
Input: quality = [1, 2, 1, 2]
Output: 2
```
Value `1` occupies indices 0 and 2 while value `2` occupies indices 1 and 3, so the two interleave and cannot both survive. Whichever value is kept, the other value's two elements must be rewritten, for a total cost of 2.
**Example 3**
```text
Input: quality = [4, 4, 7, 7, 9]
Output: 0
```
Every value already forms one contiguous block, so no operation is needed.
Constraints
- 0 <= len(quality) <= 200000
- -10^9 <= quality[i] <= 10^9
- 0 <= returned cost <= len(quality) <= 200000; the answer fits in a signed 64-bit integer
Examples
Input: ([1, 2, 1, 3, 3],)
Expected Output: 1
Input: ([1, 2, 1, 2],)
Expected Output: 2
Hints
- Two values can both survive only if their occurrences never interleave. What does the pair (first occurrence index, last occurrence index) tell you about whether two values interleave?
- Compress every distinct value to the interval [first index, last index] plus its occurrence count, then sort by first index and merge intervals that overlap.
- Inside one merged group the only remaining decision is which value to keep; the elements you keep are the ones you never pay to rewrite.