-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1122-RelativeSortArray.cs
50 lines (45 loc) · 1.31 KB
/
1122-RelativeSortArray.cs
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
41
42
43
44
45
46
47
48
49
50
//-----------------------------------------------------------------------------
// Runtime: 236ms
// Memory Usage: 30.3 MB
// Link: https://leetcode.com/submissions/detail/330680788/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _1122_RelativeSortArray
{
public int[] RelativeSortArray(int[] arr1, int[] arr2)
{
var counts = new Dictionary<int, int>();
foreach (var num in arr1)
{
if (counts.ContainsKey(num))
counts[num]++;
else
counts[num] = 1;
}
var index = 0;
foreach (var num in arr2)
{
var count = counts[num];
while (count > 0)
{
arr1[index++] = num;
count--;
}
counts.Remove(num);
}
foreach (var key in counts.Keys.OrderBy(num => num))
{
var count = counts[key];
while (count > 0)
{
arr1[index++] = key;
count--;
}
}
return arr1;
}
}
}