【发布时间】:2011-04-14 06:17:53
【问题描述】:
问题:给定一个大小为 n 的整数的输入数组和一个大小为 k 的整数的查询数组,找到包含查询数组的所有元素且顺序相同的输入数组的最小窗口。
我尝试过以下方法。
int[] inputArray = new int[] { 2, 5, 2, 8, 0, 1, 4, 7 };
int[] queryArray = new int[] { 2, 1, 7 };
将查找所有查询数组元素在 inputArray 中的位置。
public static void SmallestWindow(int[] inputArray, int[] queryArray)
{
Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
int index = 0;
foreach (int i in queryArray)
{
HashSet<int> hash = new HashSet<int>();
foreach (int j in inputArray)
{
index++;
if (i == j)
hash.Add(index);
}
dict.Add(i, hash);
index = 0;
}
// Need to perform action in above dictionary.??
}
我有以下字典
- int 2--> 位置 {1, 3}
- int 1 --> 位置 {6}
- int 7 --> 位置 {8}
现在我想执行以下步骤来找出最小窗口
比较 int 2 位置和 int 1 位置。 As (6-3)
会像上面一样比较int 1和int 7的位置。
我无法理解如何比较字典的两个连续值。请帮忙。
【问题讨论】:
-
如果
queryArray是{ 2, 8, 0 }预期输出是什么?指数[0-4]或指数[2-4]? -
@Ani - 我认为应该是
[2-4],这是最短的。 -
是的,应该是 [2-4] 因为这是最小的窗口
-
queryArray可以多次包含相同的值吗?
标签: c# algorithm data-structures collections