File: rank-transform-of-an-array/solution.py

Date: 2026-06-06

Time: 18:38

Rank Transform of an Array — solution.py

Purpose

This file implements LeetCode problem 1331: given an array of integers, replace each element with its dense rank (1-based position in the sorted unique values). It's a self-contained solution + test module following the repo's standard layout.

Key Components

Solution.arrayRankTransform(arr) — The single method. Takes a List[int] and returns a List[int] of equal length where each element is replaced by its rank.

The implementation is a one-liner in two steps:


rank_map = {v: i + 1 for i, v in enumerate(sorted(set(arr)))}
return [rank_map[x] for x in arr]

1. sorted(set(arr)) — deduplicates and sorts, producing the rank order.

2. Dict comprehension maps each unique value to its 1-based index.

3. List comprehension replaces every original element via the lookup.

TestArrayRankTransform — 10 test cases covering: standard examples, empty input, single element, negatives, pre-sorted/reverse-sorted, all-same, and extreme values (-10^9, 10^9).

Patterns

Dependencies

Imports: typing.List (type annotation), unittest (test framework). No project-internal imports.

Imported by: The test_solution.py in this same directory, plus it appears in a large "imported by" list — that list is an artifact of the repo's shared test infrastructure, not direct code dependencies on this module.

Flow

1. set(arr) — O(n) to deduplicate.

2. sorted(...) — O(k log k) where k = unique count.

3. Dict comprehension — O(k) to build the rank map.

4. List comprehension — O(n) to produce the output by looking up each element.

Total: O(n log n) time, O(n) space.

Invariants

Error Handling

None. The method assumes valid input per LeetCode constraints. No bounds checking, no exception handling. Invalid input (non-integer elements, None) would raise at the sorted/set level with standard Python errors.

Topics to Explore

Beliefs