Sponsored
Sponsored
This approach involves sorting the array to determine the rank of each element. After sorting, unique elements are mapped to their ranks.
Time Complexity: O(N log N) due to sorting.
Space Complexity: O(N) for storing the sorted array and rank map.
1def arrayRankTransform(arr):
2 sorted_arr = sorted(arr)
3
4 rank_map = {}
5 rank = 1
6 for num in sorted_arr:
7 if num not in rank_map:
8 rank_map[num] = rank
9 rank += 1
10
11 return [rank_map[num] for num in arr]
This Python solution sorts the array to determine the ranks and uses a dictionary to map each unique element to its rank. Finally, it generates the ranked array using this mapping.
Coordinate compression is a method to map large ranges of numbers to smaller ranges, maintaining their relative order. This approach uses this idea to assign ranks.
Time Complexity: O(N log N) due to sorting and binary search operations.
Space Complexity: O(N) for rank maps.
1#include <stdio.h>
2#include
The C coordinate compression solution involves sorting the array and creating a rank map by compressing coordinates. This map is used for rank assignment efficiently.