Maximize Greatness of an Array
Real-world version: How Many Places Can Be BetteredThe statement here is our own: the same inputs, outputs and technique.
A list of whole numbers reads values. Rearrange the same values into any order you like and lay the rearrangement alongside the original.
A position betters the original when the rearrangement's value there is strictly larger than the original's.
Return the most positions that can be made to better the original at once.
Examples
Example 1
- Input
- values = [2, 2, 2]
- Output
- 0
Every value is the same, so no rearrangement puts a larger value anywhere.
Example 2
- Input
- values = [1, 1, 2, 2]
- Output
- 2
The two twos can be laid over the two ones, bettering both of those positions, but nothing is left to better a two.
Example 3
- Input
- values = [5, 4, 3, 2, 1]
- Output
- 4
Shifting every value one place along leaves each position holding the next value up, which betters all but one.
Constraints
- 1 <= values.length <= 10^5
- 0 <= values[i] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def maximize_greatness(values: list[int]) -> int:public int maximizeGreatness(int[] values)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's