-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelative Sort Array
40 lines (38 loc) · 962 Bytes
/
Relative Sort Array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Solution {
public int[] RelativeSortArray(int[] arr1, int[] arr2) {
var dict = new Dictionary<int, int>();
var result = new int[arr1.Length];
foreach(int elem in arr1)
{
if(dict.ContainsKey(elem))
dict[elem]++;
else
dict.Add(elem,1);
}
int i =0;
foreach(int elem in arr2)
{
while(dict[elem] >0)
{
result[i] = elem;
i++;
dict[elem]--;
}
dict.Remove(elem);
}
var pq = new PriorityQueue<int,int>();
foreach(var kv in dict)
{ while(dict[kv.Key] >0)
{
pq.Enqueue(kv.Key, kv.Key);
dict[kv.Key]--;
}
}
while(i < arr1.Length)
{
result[i] = pq.Dequeue();
i++;
}
return result;
}
}