-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path0599-MinimumIndexSumOfTwoLists.cs
42 lines (36 loc) · 1.24 KB
/
0599-MinimumIndexSumOfTwoLists.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
//-----------------------------------------------------------------------------
// Runtime: 296ms
// Memory Usage: 41.8 MB
// Link: https://leetcode.com/submissions/detail/344732795/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0599_MinimumIndexSumOfTwoLists
{
public string[] FindRestaurant(string[] list1, string[] list2)
{
if (list1.Length > list2.Length) return FindRestaurant(list2, list1);
var map = new Dictionary<string, int>();
for (int i = 0; i < list1.Length; i++)
map[list1[i]] = i;
var result = new List<string>();
var sum = int.MaxValue;
for (int i = 0; i < list2.Length; i++)
{
if (!map.ContainsKey(list2[i]))
continue;
var cur = map[list2[i]] + i;
if (cur < sum)
{
sum = cur;
result.Clear();
result.Add(list2[i]);
}
else if (cur == sum)
result.Add(list2[i]);
}
return result.ToArray();
}
}
}