The Indian Engineer

Problem 1331 Rank Transform of an Array

Posted on 5 mins

Array Hash-Table Sorting

Problem Statement

Link - Problem 1331

Question

Given an array of integers arr, replace each element with its rank.

The rank represents how large the element is. The rank has the following rules:

Example 1:

Input: arr = [40,10,20,30]
Output: [4,1,2,3]
Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.

Example 2:

Input: arr = [100,100,100]
Output: [1,1,1]
Explanation: Same elements share the same rank.

Example 3:

Input: arr = [37,12,28,9,100,56,80,5,12]
Output: [5,3,4,2,8,6,7,1,3]

Constraints:

Solution

class Solution {
public:
    vector<int> arrayRankTransform(vector<int>& arr) {
        map<int,int>ind;
        vector<int>temp = arr;
        sort(temp.begin(),temp.end());
        int rank = 1;
        for(int i=0; i<temp.size(); i++){
            if(i>0 && temp[i]>temp[i-1])
                rank++;

            ind[temp[i]] = rank;
        }

        for(int i=0; i<arr.size(); i++)
            arr[i] = ind[arr[i]];

        return arr;
    }
};

Complexity Analysis

| Algorithm        | Time Complexity | Space Complexity |
| ---------------- | --------------- | ---------------- |
| Modified Sorting | O(n log n)      | O(n)             |

Explanation

Intial Thoughts

The problem requires replacing each element in the array with its rank. To solve this, think of ranking students based on their scores, where students with the same score get the same rank, and the next student with a higher score gets a rank that’s one more than the previous highest rank. This requires sorting and then mapping each unique score to its corresponding rank. Consider using a data structure that allows for efficient sorting and mapping, such as arrays or hash maps.

Intuitive Analysis

Intuitively, solve this problem by sorting the array and assigning ranks to each unique element. Think of a ladder where each rung represents a rank, and elements with the same value are on the same rung. The next element with a higher value climbs to the next rung, and the rank increases accordingly. Break down the solution into steps, such as creating a temporary sorted array, mapping each unique element to its rank, and finally replacing each element in the original array with its corresponding rank. Consider the constraints and how they affect the solution, such as the array size and the range of element values.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Easy difficulty.

Hints

Use a temporary array to copy the array and sort it.

The rank of each element is the number of unique elements smaller than it in the sorted array plus one.


Similar Questions:

Title URL Difficulty
Rank Transform of a Matrix https://leetcode.com/problems/rank-transform-of-a-matrix Hard
Find Target Indices After Sorting Array https://leetcode.com/problems/find-target-indices-after-sorting-array Easy