-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
40 lines (38 loc) · 985 Bytes
/
Solution.cs
File metadata and controls
40 lines (38 loc) · 985 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
32
33
34
35
36
37
38
39
40
public class Solution
{
public int MinimumDistance(int[] nums)
{
int n = nums.Length;
int[] indexs = new int[n];
for (int i = 0; i < n; i++)
{
indexs[i] = i;
}
Array.Sort(indexs, (a, b) =>
{
if (nums[a] == nums[b]) return a - b;
return nums[a] - nums[b];
});
int ans = n + 1;
int count = 1;
List<int> candidate = [indexs[0]];
for (int i = 1; i < n; i++)
{
if (nums[indexs[i]] == nums[indexs[i - 1]])
{
count++;
candidate.Add(indexs[i]);
if (count >= 3)
{
ans = Math.Min(ans, candidate[count - 1] - candidate[count - 3]);
}
}
else
{
count = 1;
candidate = [indexs[i]];
}
}
return ans == n + 1 ? -1 : 2 * ans;
}
}