Skip to content

Commit

Permalink
Create RankTransform.java
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 authored Oct 2, 2024
1 parent c78465b commit 94de23c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Leetcode/RankTransform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
1331. Rank Transform of an Array
Solved
Easy
Topics
Companies
Hint
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:
Rank is an integer starting from 1.
The larger the element, the larger the rank. If two elements are equal, their rank must be the same.
Rank should be as small as possible.
**/
class Solution {
public int[] arrayRankTransform(int[] arr) {
int n = arr.length,i=0;
TreeMap<Integer,Integer> map = new TreeMap<>();
for(i=0;i<n;i++){
map.put(arr[i],1);
}
i=1;
for(Map.Entry<Integer,Integer> entry:map.entrySet()){
map.put(entry.getKey(),i);
i++;
}
for(i=0;i<n;i++){
arr[i] = map.get(arr[i]);
}
return arr;
}
}

0 comments on commit 94de23c

Please sign in to comment.