All problems
1107EasyArraySortingCounting SortBubble Sort

Jars Standing in the Wrong Place

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1051Height Checker

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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