Height Checker
Real-world version: Jars Standing in the Wrong PlaceThe statement here is our own: the same inputs, outputs and technique.
A shelf holds jars whose sizes are listed in sizes, left to right. The jars are meant to stand in non-decreasing order of size.
Return how many positions on the shelf hold a jar of a different size from the one that would stand there once the jars were put in that order.
Examples
Example 1
- Input
- sizes = [2, 1, 2, 1]
- Output
- 2
In size order the jars would stand 1, 1, 2, 2. Every one of the four positions holds a different size from that.
Example 2
- Input
- sizes = [1, 3, 2, 4, 6, 5]
- Output
- 4
In size order the jars would run 1 up to 6. The second and third positions are the wrong way round and so are the last two.
Example 3
- Input
- sizes = [1, 1, 1, 1]
- Output
- 0
Every jar is the same size, so the shelf already stands in size order.
Constraints
- 1 <= sizes.length <= 100
- 1 <= sizes[i] <= 100
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 height_checker(sizes: list[int]) -> int:public int heightChecker(int[] sizes)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