Show problems as
Classic problem #1051EasyArraySortingCounting SortBubble Sort

Height Checker

Real-world version: Jars Standing in the Wrong Place
Tracked in this browser only
Read Height Checker on LeetCode(opens in a new tab)

The 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.

Python
def height_checker(sizes: list[int]) -> int:
Java
public int heightChecker(int[] sizes)