Sorting · Easy
A report ranks records by numeric value but must attach each rank back to the original record. Equal values receive consecutive ranks in original arrival order. Shared competition ranks are not used.
Return each original record’s zero-based position in a stable ascending ordering of all records.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[] }
Fields
- nums is an array of signed integers. Repeated values at different positions are separate entries.
Output
Return value
- Return an integer array aligned with the original input. Its entries form a permutation of 0 through n - 1.
- Equal values get different consecutive ranks; the earlier original index gets the smaller rank.
Constraints
- 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
- All totals and counts fit exactly in a safe JSON integer.
Examples
Example 1
{
"nums": [
5,
2,
5
]
}Returns
[
1,
0,
2
]Example 2
{
"nums": [
3,
2,
1
]
}Returns
[
2,
1,
0
]Required complexity
O(n log(n + 1)) time and O(n) auxiliary space