cs: add «1331. Rank Transform of an Array»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-10-02 21:22:03 +02:00
parent 133298c92a
commit 9c8e14695c
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,14 @@
public class Solution {
public int[] ArrayRankTransform(int[] arr) {
var unique = new SortedSet<int>();
foreach (var x in arr) {
unique.Add(x);
}
var ranks = unique.Select((item, index) => (item, index + 1)).ToDictionary();
for (int i = 0; i < arr.Length; ++i) {
arr[i] = ranks[arr[i]];
}
return arr;
}
}