Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created February 4, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/c26de6387245a116951412cf2208820f to your computer and use it in GitHub Desktop.
Save Desolve/c26de6387245a116951412cf2208820f to your computer and use it in GitHub Desktop.
1331 Rank Transform of an Array
class Solution {
public int[] arrayRankTransform(int[] arr) {
int[] sArr = Arrays.copyOf(arr, arr.length);
Arrays.sort(sArr);
HashMap<Integer, Integer> rk = new HashMap<>();
for (int n: sArr) rk.putIfAbsent(n, rk.size() + 1);
for (int i = 0; i < arr.length; ++i) arr[i] = rk.get(arr[i]);
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment