Show problems as
Classic problem #1331EasyArrayHash TableSorting

Rank Transform of an Array

Real-world version: Core Sample Rank Labels
Tracked in this browser only
Read Rank Transform of an Array on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A survey crew drills a line of core samples and logs the elevation of each sample's bedrock contact in metres against the site datum, so a logged value may be below the datum, on it, or above it. The log arrives as readings in drilling order. A shift that drilled nothing at all leaves the log empty.

The written report hides the raw elevations and prints rank labels instead. Labels are assigned like this:

  • The deepest elevation on the log is labelled 1.
  • Samples logging the identical elevation carry the identical label.
  • Labels run without gaps: the next elevation upwards from a labelled one takes the next whole number.

Return the labels in drilling order, one per reading.

Examples

Example 1

Input
readings = [-15, 40, -15, 8]
Output
[1, 3, 1, 2]

Three distinct elevations appear, -15 then 8 then 40, so they take labels 1, 2 and 3, and the repeated -15 keeps label 1 in both positions.

Example 2

Input
readings = [0, -4, -4, 7, 7, 12]
Output
[2, 1, 1, 3, 3, 4]

The distinct elevations run -4, 0, 7, 12, so 0 lands on label 2 while each repeated elevation reuses its own label.

Example 3

Input
readings = [-7, -8]
Output
[2, 1]

Only two samples were drilled and -8 is the deeper of the pair, so it takes label 1 and -7 takes label 2.

Constraints

  • 0 <= readings.length <= 10^5
  • -10^9 <= readings[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.

Python
def rank_core_samples(readings: list[int]) -> list[int]:
Java
public int[] rankCoreSamples(int[] readings)