Created
February 4, 2020 15:24
-
-
Save Desolve/c26de6387245a116951412cf2208820f to your computer and use it in GitHub Desktop.
1331 Rank Transform of an Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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