cs: add «1331. Rank Transform of an Array»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
133298c92a
commit
9c8e14695c
1 changed files with 14 additions and 0 deletions
14
cs/rank-transform-of-an-array.cs
Normal file
14
cs/rank-transform-of-an-array.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue