Rank Transform
Rank Transform
Rank transform replaces each value in a dataset with its rank (position when sorted in ascending order). This is a non-parametric transformation that makes the data uniformly distributed and completely removes the effect of outliers. It is used in statistics (Spearman correlation is Pearson correlation of ranks) and as a preprocessing step when the magnitude of values is less important than their relative ordering.
Given a list of values, return their 1-based ranks. When values are tied, assign the average of the ranks they would occupy.
Example
For values [1, 2, 2, 3], the two 2s occupy positions 2 and 3, so they both get rank (2+3)/2 = 2.5.
Examples
Input:
values = [10, 30, 20]
Output:
[1.0, 3.0, 2.0]
10 is the smallest (rank 1), 20 is next (rank 2), 30 is largest (rank 3).
Input:
values = [1, 2, 2, 3]
Output:
[1.0, 2.5, 2.5, 4.0]
The two 2s would occupy ranks 2 and 3. Their average rank is 2.5.
Hint 1
Sort the indices by their corresponding values. Then iterate through the sorted indices, grouping consecutive entries with equal values. For each group, compute the average of the positions they span.
Hint 2
If a group of tied values starts at sorted position i and ends at position j (1-based), the average rank is (i + i+1 + ... + j) / count, which simplifies to (i + j) / 2.
Requirements
- Assign 1-based ranks in ascending order (smallest value gets rank 1)
- For tied values, assign the average of the ranks they would occupy
- Preserve the original order of the input (rank[i] corresponds to values[i])
- Return a list of floats
Constraints
- values has at least 1 element
- Values can be negative or zero
- Return a list of floats
- Time limit: 300 ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Rank Transform
Rank Transform
Rank transform replaces each value in a dataset with its rank (position when sorted in ascending order). This is a non-parametric transformation that makes the data uniformly distributed and completely removes the effect of outliers. It is used in statistics (Spearman correlation is Pearson correlation of ranks) and as a preprocessing step when the magnitude of values is less important than their relative ordering.
Given a list of values, return their 1-based ranks. When values are tied, assign the average of the ranks they would occupy.
Example
For values [1, 2, 2, 3], the two 2s occupy positions 2 and 3, so they both get rank (2+3)/2 = 2.5.
Examples
Input:
values = [10, 30, 20]
Output:
[1.0, 3.0, 2.0]
10 is the smallest (rank 1), 20 is next (rank 2), 30 is largest (rank 3).
Input:
values = [1, 2, 2, 3]
Output:
[1.0, 2.5, 2.5, 4.0]
The two 2s would occupy ranks 2 and 3. Their average rank is 2.5.
Hint 1
Sort the indices by their corresponding values. Then iterate through the sorted indices, grouping consecutive entries with equal values. For each group, compute the average of the positions they span.
Hint 2
If a group of tied values starts at sorted position i and ends at position j (1-based), the average rank is (i + i+1 + ... + j) / count, which simplifies to (i + j) / 2.
Requirements
- Assign 1-based ranks in ascending order (smallest value gets rank 1)
- For tied values, assign the average of the ranks they would occupy
- Preserve the original order of the input (rank[i] corresponds to values[i])
- Return a list of floats
Constraints
- values has at least 1 element
- Values can be negative or zero
- Return a list of floats
- Time limit: 300 ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array