-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
51 lines (48 loc) · 1.58 KB
/
Solution.cs
File metadata and controls
51 lines (48 loc) · 1.58 KB
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
51
namespace LeetCode.Problem1122{
//1122. Relative Sort Array
//https://leetcode.com/problems/relative-sort-array/
/*
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2.
Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.
*/
public class Solution {
public int[] RelativeSortArray(int[] arr1, int[] arr2) {
List<int> ans = new();
Dictionary<int,int> dic = new Dictionary<int,int>();
{
for (int i = 0; i < arr1.Length; i++)
{
if (!dic.ContainsKey(arr1[i]))
dic.Add(arr1[i],1);
else
dic[arr1[i]]++;
}
for (int i = 0; i < arr2.Length; i++)
{
// ans.Add(arr2[i]);
if (dic.ContainsKey(arr2[i]))
{
while (dic[arr2[i]] > 0)
{
dic[arr2[i]]--;
ans.Add(arr2[i]);
}
dic.Remove(arr2[i]);
}
}
while (dic.Count > 0)
{
var val = dic.OrderBy(p => p.Key).FirstOrDefault();
while (dic[val.Key] > 0)
{
ans.Add(val.Key);
dic[val.Key]--;
}
dic.Remove(val.Key);
}
}
return ans.ToArray();
}
}
}