-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
31 lines (29 loc) · 745 Bytes
/
Solution.cs
File metadata and controls
31 lines (29 loc) · 745 Bytes
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
public class Solution
{
public string[] FindRestaurant(string[] list1, string[] list2)
{
int min = int.MaxValue;
List<string> ret = [];
Dictionary<string, int> map = [];
for (int i = 0; i < list2.Length; i++)
{
map[list2[i]] = i;
}
for (int i = 0; i < list1.Length; i++)
{
if (map.TryGetValue(list1[i], out int idx))
{
if (min > i + idx)
{
min = i + idx;
ret = [list1[i]];
}
else if (min == i + idx)
{
ret.Add(list1[i]);
}
}
}
return [.. ret];
}
}