【发布时间】:2018-10-06 15:56:13
【问题描述】:
如果给我三个长度相等的数组。每个阵列代表我在公路旅行中到特定景点的距离(即第一个阵列仅是主题公园,第二个阵列仅是博物馆,第三个是仅海滩)。我不想确定每次旅行中所有可能的旅行都停在每种类型的景点之一,从不倒车,从不两次访问同一个景点。
IE 如果我有以下三个数组: [29 50] [61 37] [37 70]
该函数将返回 3,因为可能的组合为:(29,61,70)(29,37,70)(50,61,70)
到目前为止我得到了什么: 公共 int test(int[] A, int[] B, int[] C) {
int firstStop = 0;
int secondStop = 0;
int thirdStop = 0;
List<List<int>> possibleCombinations = new List<List<int>>();
for(int i = 0; i < A.Length; i++)
{
firstStop = A[i];
for(int j = 0; j < B.Length; j++)
{
if(firstStop < B[j])
{
secondStop = B[j];
for(int k = 0; k < C.Length; k++)
{
if(secondStop < C[k])
{
thirdStop = C[k];
possibleCombinations.Add(new List<int>{firstStop, secondStop, thirdStop});
}
}
}
}
}
return possibleCombinations.Count();
}
这适用于以下测试用例:
示例测试:([29, 50], [61, 37], [37, 70]) OK 返回 3
示例测试:([5], [5], [5]) OK 返回 0
示例测试:([61, 62], [37, 38], [29, 30]) 失败返回 0
正确计算此值的正确算法是什么? 性能最好的算法是什么?
如何判断这个算法的时间复杂度的表现(即O(N*log(N))?)
【问题讨论】:
标签: c# algorithm function array-algorithms