← 返回 amazon 的题目列表Make Equal Product Quality Values Contiguous
类型:online_judge
Given an integer array quality of length n, where quality[i] is the quality value of product i.
You may repeatedly perform the following operation:
Choose two distinct quality values x and y.
Change every occurrence of x in the current array to y.
The operation costs the number of elements currently equal to x.
Find the minimum total cost required so that, in the final array, every quality value that remains appears in exactly one contiguous block. In other words, for every value v, all indices i such that quality[i] = v must form a contiguous interval.
Input Format
n
quality[0] quality[1] ... quality[n-1]
Output Format
The minimum total cost
Constraints
1 <= n <= 2 * 10^5
quality[i] is an integer.
Example 1
Input:
5
1 2 1 2 3
Output:
2
Explanation: Change every 2 to 1 at cost 2, producing [1, 1, 1, 1, 3].
Example 2
Input:
7
1 2 3 2 4 3 5
Output:
3
Explanation: The occurrence intervals of 2, 3, and 4 overlap transitively. Keep either 2 or 3, each of which occurs twice, and rewrite the other values in this connected group. The cost is 7 - 2 - 2 = 3.
Example
Input
5
1 2 1 2 3
Output
2